Version: 2020.1
言語: 日本語
public static RaycastHit2D Raycast (Vector2 origin, Vector2 direction, float distance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, float minDepth= -Mathf.Infinity, float maxDepth= Mathf.Infinity);

パラメーター

origin 2D 空間上のレイの原点
direction A vector representing the direction of the ray.
distance The maximum distance over which to cast the ray.
layerMask 特定のレイヤーのコライダーのみを判別するためのフィルター
minDepth この値以上の Z 座標(深度)を持つオブジェクトのみを含みます。
maxDepth この値以下の Z 座標(深度)を持つオブジェクトのみを含みます。

戻り値

RaycastHit2D 投げかけた結果が返されます。

説明

Casts a ray against colliders in the Scene.

Raycast は例えるならば、空間上のある地点から特定方向へ発射されたセンサーのようなものです。センサーと接触したすべてのオブジェクトは検知され報告されます。

この関数はレイがヒットするコライダーへの参照をもつ RaycastHit オブジェクトを返します。何もヒットしない場合は、結果のコライダープロパティーは null になります。layerMask を使用すると、特定のレイヤーのオブジェクトだけを検出できます(例えば敵キャラクターのみ検出するように設定できます)。

Overloads of this method that use contactFilter can filter the results by the options available in ContactFilter2D.

Raycast は視界の範囲の判定、銃弾によりターゲットの当たり判定、さらに多くのゲームシーンで役に立ちます。

Additionally, this will also detect Collider(s) at the start of the ray. In this case, the ray starts inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated, in which case the returned collision normal is set to the inverse of the ray vector being tested. This can easily be detected because such results are always at a RaycastHit2D fraction of zero.

関連項目: LayerMask クラス、RaycastHit2D クラス、RaycastAllLinecastDefaultRaycastLayersIgnoreRaycastLayerraycastsHitTriggers.

using UnityEngine;

public class Example : MonoBehaviour { // Float a rigidbody object a set distance above a surface.

public float floatHeight; // Desired floating height. public float liftForce; // Force to apply when lifting the rigidbody. public float damping; // Force reduction proportional to speed (reduces bouncing).

Rigidbody2D rb2D;

void Start() { rb2D = GetComponent<Rigidbody2D>(); }

void FixedUpdate() { // Cast a ray straight down. RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);

// If it hits something... if (hit.collider != null) { // Calculate the distance from the surface and the "error" relative // to the floating height. float distance = Mathf.Abs(hit.point.y - transform.position.y); float heightError = floatHeight - distance;

// The force is proportional to the height error, but we remove a part of it // according to the object's speed. float force = liftForce * heightError - rb2D.velocity.y * damping;

// Apply the force to the rigidbody. rb2D.AddForce(Vector3.up * force); } } }

public static int Raycast (Vector2 origin, Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance= Mathf.Infinity);

パラメーター

origin 2D 空間上のレイの原点
direction A vector representing the direction of the ray.
contactFilter The contact filter used to filter the results differently, such as by layer mask, Z depth, or normal angle.
results The array to receive results. The size of the array determines the maximum number of results that can be returned.
distance The maximum distance over which to cast the ray.

戻り値

int Returns the number of results placed in the results array.

説明

Casts a ray against colliders in the Scene.

Raycast は例えるならば、空間上のある地点から特定方向へ発射されたセンサーのようなものです。センサーと接触したすべてのオブジェクトは検知され報告されます。

This function returns the number of contacts found and places those contacts in the results array. The results can also be filtered by the contactFilter.

See Also: ContactFilter2D and RaycastHit2D.


public static int Raycast (Vector2 origin, Vector2 direction, ContactFilter2D contactFilter, List<RaycastHit2D> results, float distance= Mathf.Infinity);

パラメーター

origin 2D 空間上のレイの原点
direction A vector representing the direction of the ray.
contactFilter The contact filter used to filter the results differently, such as by layer mask, Z depth, or normal angle.
results The list to receive results.
distance The maximum distance over which to cast the ray.

戻り値

int Returns the number of results placed in the results list.

説明

Casts a ray against Colliders in the Scene.

A raycast is conceptually like a laser beam that is fired from a point in space along a particular direction. Any Collider making contact with the beam can be detected and reported.

The integer return value is the number of results written into the results list. The results list will be resized if it doesn't contain enough elements to report all the results. This prevents memory from being allocated for results when the results list does not need to be resized, and improves garbage collection performance when the query is performed frequently.

The results can also be filtered by the contactFilter.

See Also: ContactFilter2D and RaycastHit2D.