Version: 2018.4
LanguageEnglish
  • C#

Physics.RaycastNonAlloc

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static int RaycastNonAlloc(Ray ray, RaycastHit[] results, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters

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.

Returns

int The amount of hits stored into the results buffer.

Description

Cast a ray through the Scene and store the hits into the buffer.

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.


Declaration

public static int RaycastNonAlloc(Vector3 origin, Vector3 direction, RaycastHit[] results, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters

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.
queryTriggerInteraction Specifies whether this query should hit Triggers.
layerMask A Layer mask that is used to selectively ignore colliders when casting a ray.

Returns

int The amount of hits stored into the results buffer.

Description

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];

// See Order of Execution for Event Functions for information on FixedUpdate() and Update() related to physics queries void FixedUpdate() { // Set the layer mask to all layers var layerMask = ~0;

int hits = Physics.RaycastNonAlloc(transform.position, transform.forward, m_Results, Mathf.Infinity, layerMask); for (int i = 0; i < hits; i++) { Debug.Log("Hit " + m_Results[i].collider.gameObject.name); } if (hits == 0) { Debug.Log("Did not hit"); } } }