Skip to main content

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​

NameTypeDescription
allowedSizeintThe 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​

NameTypeDescription
rayRayThe ray to cast.
sortByDistanceboolIf true, sorts hits by distance from the ray origin.
maxDistancefloatThe maximum distance to cast the ray.
layerMaskint?Optional layer mask to filter hits.

Returns​

IEnumerable<T> — An enumerable of components of type T.

Type Parameters​

NameTypeDescription
TType 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​

NameTypeDescription
originVector3The starting point of the ray.
directionVector3The direction of the ray.
sortByDistanceboolIf true, sorts hits by distance from the origin.
maxDistancefloatThe maximum distance to cast the ray.
layerMaskint?Optional layer mask to filter hits.

Returns​

IEnumerable<T> — An enumerable of components of type T.

Type Parameters​

NameTypeDescription
TType 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​

NameTypeDescription
originVector3The starting point of the sphere cast.
radiusfloatThe radius of the sphere cast.
directionVector3The direction to cast the sphere.
sortByDistanceboolIf true, sorts hits by distance from the origin.
maxDistancefloatThe maximum distance to cast the sphere.
layerMaskint?Optional layer mask to filter hits.

Returns​

IEnumerable<T> — An enumerable of components of type T.

Type Parameters​

NameTypeDescription
TType 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​

NameTypeDescription
rayRayThe ray to cast the sphere along.
radiusfloatThe radius of the sphere cast.
sortByDistanceboolIf true, sorts hits by distance from the ray origin.
maxDistancefloatThe maximum distance to cast the sphere.
layerMaskint?Optional layer mask to filter hits.

Returns​

IEnumerable<T> — An enumerable of components of type T.

Type Parameters​

NameTypeDescription
TType 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​

NameTypeDescription
rayRayThe ray to cast.
sortByDistanceboolIf true, sorts hits by distance from the ray origin.
maxDistancefloatThe maximum distance to cast the ray.
layerMaskint?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​

NameTypeDescription
originVector3The starting point of the ray.
directionVector3The direction of the ray.
sortByDistanceboolIf true, sorts hits by distance from the origin.
maxDistancefloatThe maximum distance to cast the ray.
layerMaskint?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​

NameTypeDescription
originVector3The starting point of the sphere cast.
radiusfloatThe radius of the sphere cast.
directionVector3The direction to cast the sphere.
sortByDistanceboolIf true, sorts hits by distance from the origin.
maxDistancefloatThe maximum distance to cast the sphere.
layerMaskint?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​

NameTypeDescription
rayRayThe ray to cast the sphere along.
radiusfloatThe radius of the sphere cast.
sortByDistanceboolIf true, sorts hits by distance from the ray origin.
maxDistancefloatThe maximum distance to cast the sphere.
layerMaskint?Optional layer mask to filter hits.

Returns​

IEnumerable<RaycastHit> — An enumerable of RaycastHit objects.

  • Examples
  • Remarks
  • Constructors
    • BufferedRaycast(int)
  • Methods
    • FilteredRaycast<T>(Ray, bool, float, int?)
    • FilteredRaycast<T>(Vector3, Vector3, bool, float, int?)
    • FilteredSphereCast<T>(Vector3, float, Vector3, bool, float, int?)
    • FilteredSphereCast<T>(Ray, float, bool, float, int?)
    • Raycast(Ray, bool, float, int?)
    • Raycast(Vector3, Vector3, bool, float, int?)
    • SphereCast(Vector3, float, Vector3, bool, float, int?)
    • SphereCast(Ray, float, bool, float, int?)