ray | The starting point and direction of the ray. |
results | The buffer to store the hits into. |
maxDistance | The max distance the rayhit is allowed to be from the start of the ray. |
layerMask | A Layer mask that is used to selectively ignore colliders when casting a ray. |
queryTriggerInteraction | Specifies whether this query should hit Triggers. |
int
The amount of hits stored into the results
buffer.
Cast a ray through the scene and store the hits into the buffer.
Like Physics.RaycastAll, but generates no garbage.
origin | The starting point and direction of the ray. |
results | The buffer to store the hits into. |
direction | The direction of the ray. |
maxDistance | The max distance the rayhit is allowed to be from the start of the ray. |
layermask | A Layer mask that is used to selectively ignore colliders when casting a ray. |
queryTriggerInteraction | Specifies whether this query should hit Triggers. |
int
The amount of hits stored into the results
buffer.
Cast a ray through the scene and store the hits into the buffer.
using UnityEngine;
public class ExampleClass : MonoBehaviour { // The size of the array determines how many raycasts will occur RaycastHit[] m_Results = new RaycastHit[5];
void Update() { // Set the layer mask to all layers var layerMask = ~0;
// Do any of the rays hit? if (Physics.RaycastNonAlloc(transform.position, transform.forward, m_Results, Mathf.Infinity, layerMask) > 0) { foreach (var result in m_Results) { // Check for null since some array spots might be if (result.collider != null) { Debug.Log("Hit " + result.collider.gameObject.name); } } } else { Debug.Log("Did not hit"); } } }
Did you find this page useful? Please give it a rating: