RaycastNonAlloc
Cast a ray through the Scene and store the hits into the buffer.
Read time 3 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.PhysicsModule
RaycastNonAlloc(Ray, RaycastHit[], float, int, QueryTriggerInteraction)
Cast a ray through the Scene and store the hits into the buffer.
public static int RaycastNonAlloc(Ray ray, RaycastHit[] results, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
Parameters
The starting point and direction of the ray.
The buffer to store the hits into.
The max distance the rayhit is allowed to be from the start of the ray.
A Layer mask that is used to selectively filter which colliders are considered when casting a ray.
Specifies whether this query should hit Triggers.
Returns
Type | Description |
|---|---|
| int | The amount of hits stored into the |
Remarks
Like Physics.RaycastAll, but generates no garbage.
The raycast query ends when there are no more hits and/or the results buffer is full. The order of the results is undefined. When a full buffer is returned it is not guaranteed that the results are the closest hits and the length of the buffer is returned. If a null buffer is passed in, no results are returned and no errors or exceptions are thrown.
RaycastNonAlloc(Vector3, Vector3, RaycastHit[], float, int, QueryTriggerInteraction)
Cast a ray through the Scene and store the hits into the buffer.
public static int RaycastNonAlloc(Vector3 origin, Vector3 direction, RaycastHit[] results, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
Parameters
The starting point and direction of the ray.
The direction of the ray.
The buffer to store the hits into.
The max distance the rayhit is allowed to be from the start of the ray.
A Layer mask that is used to selectively filter which colliders are considered when casting a ray.
Specifies whether this query should hit Triggers.
Returns
Type | Description |
|---|---|
| int | The amount of hits stored into the |
Examples
using UnityEngine;public class RaycastFanNonAlloc : MonoBehaviour{ public int rayCount = 10; // Number of rays in the fan public float angle = 60f; // Total spread angle in degrees public float maxDistance = 20f; // Ray length // The size of the array determines how many raycasts will occur RaycastHit[] m_Results = new RaycastHit[5]; // Reused buffer to avoid GC allocations // See https://docs.unity.com/engine/6000.7/manual/scripting/managing-update-order/execution-order for information on FixedUpdate() and Update() related to physics queries void FixedUpdate() { Vector3 origin = transform.position; Vector3 forward = transform.forward; float halfAngle = angle / 2f; for (int i = 0; i < rayCount; i++) { // Interpolate angle across the spread range float lerp = (float)i / (rayCount - 1); float currentAngle = Mathf.Lerp(-halfAngle, halfAngle, lerp); // Rotate direction around Y axis Quaternion rotation = Quaternion.AngleAxis(currentAngle, Vector3.up); Vector3 direction = rotation * forward; // Note: The buffer is overwritten from index 0 up to the number of hits returned. Unused slots remain unchanged. int hits = Physics.RaycastNonAlloc(origin, direction, m_Results, maxDistance); if (hits > 0) { for (int j = 0; j < hits; j++) { Debug.Log($"Ray {i} hit {m_Results[j].collider.gameObject.name}"); Debug.DrawLine(origin, m_Results[j].point, Color.green); } } else { Debug.DrawRay(origin, direction * maxDistance, Color.red); } } }}