Class BufferedRaycast
Zero-allocation wrapper around Unity's Physics.RaycastNonAlloc and
Physics.SphereCastNonAlloc APIs that stores results in a fixed-size array
allocated once at construction time.
public class BufferedRaycast
Inheritance
Examples
Allocate the instance once at component startup so the internal hit and distance arrays
are never re-created at runtime. Call Int32}) with
sortByDistance: true in the per-frame hot path; the results are returned via a
zero-allocation iterator over the pre-allocated buffer rather than a freshly constructed
RaycastHit[] as Physics.RaycastAll would produce.
public class GazeCaster : MonoBehaviour
{
// Allocated once; supports up to 10 simultaneous hits per cast — matching
// the capacity used by VarjoEyeTrackingHandler for 200 Hz gaze sampling.
private readonly BufferedRaycast _raycast = new BufferedRaycast(10);
private void Update()
{
Ray gazeRay = new Ray(transform.position, transform.forward);
// sortByDistance: true orders hits nearest-first with no List allocation;
// the sort is performed in-place on the internal float[] distance buffer.
foreach (RaycastHit hit in _raycast.Raycast(gazeRay, sortByDistance: true))
{
// Process hits in ascending distance order.
Debug.Log($"Hit {hit.collider.name} at {hit.distance:F2} m");
}
// No GC pressure: _raycast reuses the same RaycastHit[] on every call.
}
}
Remarks
Unity's convenience overloads (Physics.RaycastAll, Physics.SphereCastAll)
return a newly allocated RaycastHit[] on every call. At the 200 Hz gaze-logging
rate used by VarjoEyeTrackingHandler this produces 200 allocations per
second, increasing GC pause frequency. BufferedRaycast fixes the array size
at construction — the caller trades dynamic result counts for zero per-cast heap pressure.
The optional distance sort uses a companion float[] buffer and
Array) on the slice that was actually filled, avoiding allocating a
List for the sort step.
Constructors
BufferedRaycast(int)
Initializes a new instance of the BufferedRaycast class,
allocating buffers for up to allowedSize hits.
public BufferedRaycast(int allowedSize)
Parameters
| Name | Type | Description |
|---|---|---|
allowedSize | int | The maximum number of hits to store per cast. |
Methods
FilteredRaycast<T>(Ray, bool, float, int?)
Performs a raycast and returns components of type T
attached to GameObjects hit by the raycast.
public IEnumerable<T> FilteredRaycast<T>(Ray ray, bool sortByDistance = false, float maxDistance = Infinity, int? layerMask = null)
Parameters
| Name | Type | Description |
|---|---|---|
ray | Ray | The ray to cast. |
sortByDistance | bool | If true, sorts hits by distance from the ray origin. |
maxDistance | float | The maximum distance to cast the ray. |
layerMask | int? | Optional layer mask to filter hits. |
Returns
IEnumerable<T> — An enumerable of components of type T.
Type Parameters
| Name | Type | Description |
|---|---|---|
T | Type of component to retrieve from hit GameObjects. |
FilteredRaycast<T>(Vector3, Vector3, bool, float, int?)
Performs a raycast and returns components of type T
attached to GameObjects hit by the raycast.
public IEnumerable<T> FilteredRaycast<T>(Vector3 origin, Vector3 direction, bool sortByDistance = false, float maxDistance = Infinity, int? layerMask = null)
Parameters
| Name | Type | Description |
|---|---|---|
origin | Vector3 | The starting point of the ray. |
direction | Vector3 | The direction of the ray. |
sortByDistance | bool | If true, sorts hits by distance from the origin. |
maxDistance | float | The maximum distance to cast the ray. |
layerMask | int? | Optional layer mask to filter hits. |
Returns
IEnumerable<T> — An enumerable of components of type T.
Type Parameters
| Name | Type | Description |
|---|---|---|
T | Type of component to retrieve from hit GameObjects. |
FilteredSphereCast<T>(Vector3, float, Vector3, bool, float, int?)
Performs a sphere cast and returns components of type T
attached to GameObjects hit by the sphere cast.
public IEnumerable<T> FilteredSphereCast<T>(Vector3 origin, float radius, Vector3 direction, bool sortByDistance = false, float maxDistance = Infinity, int? layerMask = null)
Parameters
| Name | Type | Description |
|---|---|---|
origin | Vector3 | The starting point of the sphere cast. |
radius | float | The radius of the sphere cast. |
direction | Vector3 | The direction to cast the sphere. |
sortByDistance | bool | If true, sorts hits by distance from the origin. |
maxDistance | float | The maximum distance to cast the sphere. |
layerMask | int? | Optional layer mask to filter hits. |
Returns
IEnumerable<T> — An enumerable of components of type T.
Type Parameters
| Name | Type | Description |
|---|---|---|
T | Type of component to retrieve from hit GameObjects. |
FilteredSphereCast<T>(Ray, float, bool, float, int?)
Performs a sphere cast and returns components of type T
attached to GameObjects hit by the sphere cast.
public IEnumerable<T> FilteredSphereCast<T>(Ray ray, float radius, bool sortByDistance = false, float maxDistance = Infinity, int? layerMask = null)
Parameters
| Name | Type | Description |
|---|---|---|
ray | Ray | The ray to cast the sphere along. |
radius | float | The radius of the sphere cast. |
sortByDistance | bool | If true, sorts hits by distance from the ray origin. |
maxDistance | float | The maximum distance to cast the sphere. |
layerMask | int? | Optional layer mask to filter hits. |
Returns
IEnumerable<T> — An enumerable of components of type T.
Type Parameters
| Name | Type | Description |
|---|---|---|
T | Type of component to retrieve from hit GameObjects. |
Raycast(Ray, bool, float, int?)
Performs a raycast using the specified Ray and returns the hits.
public IEnumerable<RaycastHit> Raycast(Ray ray, bool sortByDistance = false, float maxDistance = Infinity, int? layerMask = null)
Parameters
| Name | Type | Description |
|---|---|---|
ray | Ray | The ray to cast. |
sortByDistance | bool | If true, sorts hits by distance from the ray origin. |
maxDistance | float | The maximum distance to cast the ray. |
layerMask | int? | Optional layer mask to filter raycast hits. |
Returns
IEnumerable<RaycastHit> — An enumerable of RaycastHit objects.
Raycast(Vector3, Vector3, bool, float, int?)
Performs a raycast from the given origin and direction, returning the hits.
public IEnumerable<RaycastHit> Raycast(Vector3 origin, Vector3 direction, bool sortByDistance = false, float maxDistance = Infinity, int? layerMask = null)
Parameters
| Name | Type | Description |
|---|---|---|
origin | Vector3 | The starting point of the ray. |
direction | Vector3 | The direction of the ray. |
sortByDistance | bool | If true, sorts hits by distance from the origin. |
maxDistance | float | The maximum distance to cast the ray. |
layerMask | int? | Optional layer mask to filter raycast hits. |
Returns
IEnumerable<RaycastHit> — An enumerable of RaycastHit objects.
SphereCast(Vector3, float, Vector3, bool, float, int?)
Performs a sphere cast from the given origin with the specified radius and direction, returning the hits.
public IEnumerable<RaycastHit> SphereCast(Vector3 origin, float radius, Vector3 direction, bool sortByDistance = false, float maxDistance = Infinity, int? layerMask = null)
Parameters
| Name | Type | Description |
|---|---|---|
origin | Vector3 | The starting point of the sphere cast. |
radius | float | The radius of the sphere cast. |
direction | Vector3 | The direction to cast the sphere. |
sortByDistance | bool | If true, sorts hits by distance from the origin. |
maxDistance | float | The maximum distance to cast the sphere. |
layerMask | int? | Optional layer mask to filter hits. |
Returns
IEnumerable<RaycastHit> — An enumerable of RaycastHit objects.
SphereCast(Ray, float, bool, float, int?)
Performs a sphere cast using the specified Ray and radius, returning the hits.
public IEnumerable<RaycastHit> SphereCast(Ray ray, float radius, bool sortByDistance = false, float maxDistance = Infinity, int? layerMask = null)
Parameters
| Name | Type | Description |
|---|---|---|
ray | Ray | The ray to cast the sphere along. |
radius | float | The radius of the sphere cast. |
sortByDistance | bool | If true, sorts hits by distance from the ray origin. |
maxDistance | float | The maximum distance to cast the sphere. |
layerMask | int? | Optional layer mask to filter hits. |
Returns
IEnumerable<RaycastHit> — An enumerable of RaycastHit objects.