Version: 2021.1
言語: 日本語
public static int RaycastNonAlloc (Ray ray, RaycastHit[] results, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

パラメーター

ray 始点とレイの方向
results 衝突情報を保存するバッファ
maxDistance レイヒットが発生する始点からの最大距離
layerMask レイヤーマスク はレイキャストするときに選択的に衝突を無視するために使用します。
queryTriggerInteraction トリガーに設定されているものも検索対象にするか

戻り値

int results バッファに保存された衝突情報の数

説明

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

Physics.RaycastAll と似ていますがゴミを発生させません。

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.


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

パラメーター

origin 始点とレイの方向
results 衝突情報を保存するバッファ
direction レイの方向
maxDistance レイヒットが発生する始点からの最大距離
queryTriggerInteraction トリガーに設定されているものも検索対象にするか
layerMask レイヤーマスク はレイキャストするときに選択的に衝突を無視するために使用します。

戻り値

int results バッファに保存された衝突情報の数

説明

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"); } } }