レイヤーを使用して、プロジェクトやワークフローを最適化することができます。レイヤーの一般的な用途には、以下が含まれます。 * レイヤーベースのレンダリング * レイヤーベースの衝突
カメラの カリングマスク を使用すると、特定のレイヤー上にあるオブジェクトあるいはレイヤーの選択範囲内にあるオブジェクトのみをレンダリングすることができます。カリングマスクを変更するには、使用するカメラを選択し、Inspector ウィンドウで Culling Mask ドロップダウンを選択してください。特定のレイヤーのチェックボックスをオフにすると、そのレイヤーはシーンにレンダリングされません。
ノート: UI 要素とスクリーンスペースキャンバスの子は例外で、関係なくレンダリングされます。
レイヤーを使って、レイキャストが交差できるゲームオブジェクトを指定することができます。レイキャストが特定のゲームオブジェクトを無視するようにするには、そのゲームオブジェクトを Ignore Raycast レイヤーに割り当てるか、レイキャスト API 呼び出しに レイヤーマスク を渡します。
レイキャスト API 呼び出しにレイヤーマスクを渡さない場合、Unity は Physics.DefaultRaycastLayers を使用します。これは Ignore Raycast 以外の全てのレイヤーに一致します。
Physics.Raycast 関数はビットマスクを使用します。各ビットが、レイヤーがレイによって無視されるかどうかを決定します。layerMask (レイヤーマスク) 内の全てのビットがオンである場合は、レイが全てのコライダーと衝突します。layerMask = 0 の場合は、衝突が起こりません。
例えば、レイヤー 8 にレイを投射したい場合は、以下のコードサンプルを参照してください。
int layerMask = 1 << 8;
// Does the ray intersect any objects which are in layer 8?
if (Physics.Raycast(transform.position, Vector3.forward, Mathf.Infinity, layerMask))
{
Debug.Log("The ray hit the player");
その上で、この逆を行い、レイがレイヤー 8 以外の全てのレイヤーと衝突するようにすることができます。
void Update ()
{
// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;
// This casts rays only against colliders in layer 8.
// But to collide against everything except layer 8, use the ~ operator because it inverts a bitmask.
layerMask = ~layerMask;
RaycastHit hit;
// Does the ray intersect any objects excluding layer 8.
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");
}
}
ノート: レイキャスト関数に layerMask を渡さなくても、Ignore Raycast レイヤーを使用しているコライダーは無視されます。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.