レイヤー は、カメラ がシーンの一部のみを描画するために、また、ライト がシーンの一部のみを照らすためにもっとも頻繁に使用されます。しかし、レイキャスティングが選択的にコライダーを無視したり、 衝突 を発生させるためにも使用することができます。
まず最初に、新しいレイヤーを作成し、それを ゲームオブジェクト に割り当てます。新しいレイヤーを作成するには、Tags & Layers ウィンドウ (Edit > Project Settings を開き、Tags and Layers カテゴリを選択) を開きます。
空の User Layer の中から新規レイヤーを作成します。ここではレイヤー 8 を選択することにします。
新規レイヤーを作成したら、次にゲームオブジェクトの 1 つに割り当てます。
Tags and Layers ウィンドウで、Player レイヤーを Layer 8に割り当てます。
カメラのカリングマスクを使用して、特定の 1 つのレイヤーのオブジェクトのみをレンダリングできます。 そのためには、オブジェクトを選択的に描画するカメラを選択します。
Culling Mask プロパティでレイヤーのチェックをつけたり外したりすることでカリングマスクの設定を変更することができます。
UI 要素はカリングされないことに注意してください。スクリーンスペースのキャンバスの子はカメラのカリングマスクの影響を受けません。
レイヤーを使用して、特定のレイヤーでレイキャストを行ったり、コライダーを無視することができます。 例えば 、Player レイヤーのみにレイキャストを行い、他のコライダーを無視することができます。
Physics.Raycast 関数はビットマスクを使用して、ビットごとにレイヤーを無視するかどうか判定することができます。 layerMask (レイヤーマスク)のすべてのビットが有効である場合は、すべてのコライダーと衝突します。 もし layerMask = 0 の場合、レイがコライダーに衝突することはありません。
int layerMask = 1 << 8;
//プレイヤーレイヤーにあるオブジェクトとレイが交差するかどうか。
if(Physics.Raycast(transform.position、Vector3.forward、Mathf.Infinity、layerMask))
{
Debug.Log("The ray hit the player");
}
この逆を行いたい場合もあります。Player レイヤー以外のすべてのコライダーに対してレイキャストを行います。
void Update ()
{
// ビットマスクを取得するためにレイヤー 8 のインデックスをビットシフトします。
int layerMask = 1 << 8;
// レイヤー8 のコライダーに対してのみレイを投影します。
// 代わりに、レイヤー8 以外のすべてと衝突させたい。~ 演算子はこれを行い、ビットマスクを反転します。
layerMask = ~layerMask;
RaycastHit hit;
// プレイヤーレイヤーを除くすべてのオブジェクトとレイが交差するかどうか
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");
}
}
Raycast 関数に layerMask を渡さない場合、IgnoreRaycast レイヤーのコライダーのみ無視します。 レイキャストを行うときに一部のコライダーを無視するにはこれがもっとも簡単です。
注意 Layer 31 は内部的にエディターの Preview ウィンドウ機能によって使用されます。クラッシュを避けるために、このレイヤーを使用しないでください。
2017–05–08 修正されたページ
カリングマスクの情報は Unity 2017.1 で更新
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.