Version: 2021.2
言語: 日本語
public static bool Raycast (Vector3 origin, Vector3 direction, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

パラメーター

origin ワールド座標でのレイの開始地点
direction レイの方向
maxDistance レイが衝突を検知する最大距離
layerMask レイヤーマスクはレイキャストするときに選択的に衝突を無視するために使用します。
queryTriggerInteraction トリガーに設定されているものも検索対象にするか

戻り値

bool Returns true if the ray intersects with a Collider, otherwise false.

説明

Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.

You may optionally provide a LayerMask, to filter out any Colliders you aren't interested in generating collisions with.

Specifying queryTriggerInteraction allows you to control whether or not Trigger colliders generate a hit, or whether to use the global Physics.queriesHitTriggers setting.

Notes: Raycasts will not detect Colliders for which the Raycast origin is inside the Collider. In all these examples FixedUpdate is used rather than Update. Please see Order of Execution for Event Functions to understand the difference between Update and FixedUpdate, and to see how they relate to physics queries.

using UnityEngine;

// C# example.

public class ExampleClass : MonoBehaviour { // See Order of Execution for Event Functions for information on FixedUpdate() and Update() related to physics queries void FixedUpdate() { // Bit shift the index of the layer (8) to get a bit mask int layerMask = 1 << 8;

// This would cast rays only against colliders in layer 8. // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask. layerMask = ~layerMask;

RaycastHit hit; // Does the ray intersect any objects excluding the player layer if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask)) { Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow); Debug.Log("Did Hit"); } else { Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white); Debug.Log("Did not Hit"); } } }

以下の例ではオブジェクトの現在位置から前方へ距離 10 ユニットのレイを投影するシンプルな Raycast を作成します。

using UnityEngine;

public class ExampleClass : MonoBehaviour { // See Order of Execution for Event Functions for information on FixedUpdate() and Update() related to physics queries void FixedUpdate() { Vector3 fwd = transform.TransformDirection(Vector3.forward);

if (Physics.Raycast(transform.position, fwd, 10)) print("There is something in front of the object!"); } }

public static bool Raycast (Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction);

パラメーター

origin ワールド座標でのレイの開始地点
direction レイの方向
hitInfo If true is returned, hitInfo will contain more information about where the closest collider was hit. (See Also: RaycastHit).
maxDistance レイが衝突を検知する最大距離
layerMask レイヤーマスク はレイキャストするときに選択的に衝突を無視するために使用します。
queryTriggerInteraction トリガーに設定されているものも検索対象にするか

戻り値

bool Returns true when the ray intersects any collider, otherwise false.

説明

Casts a ray against all colliders in the Scene and returns detailed information on what was hit.

以下は現在のオブジェクトとヒットしたコライダー間の距離を報告する例です。

using UnityEngine;

public class RaycastExample : MonoBehaviour { // See Order of Execution for Event Functions for information on FixedUpdate() and Update() related to physics queries void FixedUpdate() { RaycastHit hit;

if (Physics.Raycast(transform.position, -Vector3.up, out hit)) print("Found an object - distance: " + hit.distance); } }

以下はレイの投射距離を制限する maxDistance パラメータを説明する例です。

using UnityEngine;

public class RaycastExample : MonoBehaviour { // See Order of Execution for Event Functions for information on FixedUpdate() and Update() related to physics queries void FixedUpdate() { RaycastHit hit;

if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0f)) print("Found an object - distance: " + hit.distance); } }

public static bool Raycast (Ray ray, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

パラメーター

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

戻り値

bool Returns true when the ray intersects any collider, otherwise false.

説明

上記と同様ですが ray.originray.direction を、origin と direction の代わりに使用します

using UnityEngine;

public class ExampleClass : MonoBehaviour { // See Order of Execution for Event Functions for information on FixedUpdate() and Update() related to physics queries void FixedUpdate() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, 100)) print("Hit something!"); } }

public static bool Raycast (Ray ray, out RaycastHit hitInfo, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

パラメーター

ray 始点とレイの方向
hitInfo If true is returned, hitInfo will contain more information about where the closest collider was hit. (See Also: RaycastHit).
maxDistance レイが衝突を検知する最大距離
layerMask レイヤーマスク はレイキャストするときに選択的に衝突を無視するために使用します。
queryTriggerInteraction トリガーに設定されているものも検索対象にするか

戻り値

bool Returns true when the ray intersects any collider, otherwise false.

説明

上記と同様ですが ray.originray.direction を、origin と direction の代わりに使用します

以下は衝突が検知されている間レイの長さに沿った線を描画する例です。

using UnityEngine;

public class ExampleClass : MonoBehaviour { // See Order of Execution for Event Functions for information on FixedUpdate() and Update() related to physics queries void FixedUpdate() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;

if (Physics.Raycast(ray, out hit, 100)) Debug.DrawLine(ray.origin, hit.point); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961