在 Unity 中,__层__定义哪些游戏对象可以与不同的功能以及彼此交互。它们主要有两种用途:由__摄像机__用来仅渲染场景的某一部分;由__光源__用来仅照亮场景的某些部分。但是,层也可以供射线投射用于选择性地忽略碰撞体或创建碰撞。
第一步是创建一个新层,随后我们可以将这个层分配给__游戏对象__。要创建新层,请打开 Tags and Layers 窗口(主菜单:Edit > Project Settings,然后选择 Tags and Layers 类别)。
我们可以在某个空用户层中创建一个新层。我们选择第 8 层。
创建新层后,可以将该层分配给一个或多个游戏对象。
每个游戏对象只能分配一层。
在 Tags and Layers 窗口中,Player 层分配给第 8 层。
利用摄像机的剔除遮罩,您可以选择性地渲染位于某一特定层中的对象。 为此,请选择需要部分渲染对象的摄像机。
通过在剔除遮罩属性中选中或取消选中某些层来修改剔除遮罩。
请注意,不会剔除 UI 元素。屏幕空间画布子项会不受摄像机的剔除遮罩影响。
通过使用层,您可以投射光线并忽略特定层中的碰撞体。 例如,您可能希望仅对玩家层投射光线并忽略所有其他碰撞体。
Physics.Raycast 函数获取位掩码,在位掩码中,每个位确定该层是否将被忽略。 如果 layerMask 中的所有位都启用,那么将可以对所有碰撞体进行碰撞。 如果 layerMask = 0,则投射光线不会发生任何碰撞。
int layerMask = 1 << 8;
// Does the ray intersect any objects which are in the player layer.
if (Physics.Raycast(transform.position, Vector3.forward, Mathf.Infinity, layerMask))
{
Debug.Log("The ray hit the player");
}
然而,在现实世界中,您希望相反的效果。我们想要对玩家层中碰撞体之外的所有碰撞体投射光线。
void Update ()
{
// 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");
}
}
如果您不将 layerMask 传递到 Raycast 函数,则仅忽略使用 IgnoreRaycast 层的碰撞体。 这是在投射光线时忽略某些碰撞体最简单的方法。
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.