# SphereCastNonAlloc

> Cast sphere along the direction and store the results into buffer.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.0/script-reference/unityengine.md)
* **Assembly:** UnityEngine.PhysicsModule

## SphereCastNonAlloc(Vector3, float, Vector3, RaycastHit\[], float, int, QueryTriggerInteraction)

Cast sphere along the direction and store the results into buffer.

```csharp
public static int SphereCastNonAlloc(Vector3 origin, float radius, Vector3 direction, RaycastHit[] results, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
```

### Parameters

**** (\[Vector3]\(/engine/6000.0/script-reference/unityengine/vector3)): The center of the sphere at the start of the sweep.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The radius of the sphere.**** (\[Vector3]\(/engine/6000.0/script-reference/unityengine/vector3)): The direction in which to sweep the sphere.**** (\[RaycastHit\[]]\(/engine/6000.0/script-reference/unityengine/raycasthit)): The buffer to save the hits into.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The max length of the sweep.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A [Layer mask](/engine/6000.0/manual/working-with-gameobjects/layers.md) that is used to selectively ignore colliders when casting a sphere.**** (\[QueryTriggerInteraction]\(/engine/6000.0/script-reference/unityengine/querytriggerinteraction)): Specifies whether this query should hit Triggers.

### Returns

| Type                                                       | Description                                        |
| ---------------------------------------------------------- | -------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The amount of hits stored into the results buffer. |

### Remarks

This is  variant of [Physics.SphereCastAll](/engine/6000.0/script-reference/unityengine/physics/spherecastall.md), but instead of allocating the array with the results of the query, it stores the results into the user-provided array. It will only compute as many hits as fit into the buffer, and store them in no particular order. It's not guaranteed that it will store only the closest hits. Generates no garbage.

## SphereCastNonAlloc(Ray, float, RaycastHit\[], float, int, QueryTriggerInteraction)

Cast sphere along the direction and store the results into buffer.

```csharp
public static int SphereCastNonAlloc(Ray ray, float radius, RaycastHit[] results, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
```

### Parameters

**** (\[Ray]\(/engine/6000.0/script-reference/unityengine/ray)): The starting point and direction of the ray into which the sphere sweep is cast.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The radius of the sphere.**** (\[RaycastHit\[]]\(/engine/6000.0/script-reference/unityengine/raycasthit)): The buffer to save the results to.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The max length of the sweep.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A [Layer mask](/engine/6000.0/manual/working-with-gameobjects/layers.md) that is used to selectively ignore colliders when casting a sphere.**** (\[QueryTriggerInteraction]\(/engine/6000.0/script-reference/unityengine/querytriggerinteraction)): Specifies whether this query should hit Triggers.

### Returns

| Type                                                       | Description                                        |
| ---------------------------------------------------------- | -------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The amount of hits stored into the results buffer. |
