查看: 2812|回复: 0
打印 上一主题 下一主题

[Unity 组件参考手册]层 Layer

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-1-31 23:07:41 |只看该作者 |倒序浏览
Layers are most commonly used by Cameras to render only a part of the scene, and by Lights to illuminate only parts of the scene. But they can also used by raycasting to selectively ignore colliders or to create collisions.经常使用层的是相机和灯光,相机用它来渲染场景中的一部分物体,而灯光依靠层来仅仅照亮场景中的一部分物体。但是也经常利用层,通过光线投射来选择性地忽略碰撞器,或者添加碰撞功能。
[Creating Layers 创建层]The first step is to create a new layer, which we can then assign to a GameObject. To create a new layer, open the Edit menu and select Project Settings->Tags.首先是创建一个新的层,随后将它指派给一个GameObject(即与该GameObject关联)。创建一个新的层只需要,打开编辑菜单,选择工程设置->标签。We create a new layer in one of the empty User Layers. We choose layer 8.在空用户层里创建一个新的层,我们选曾第8层。[Assigning Layers 分配层]Now that you have created a new layer, you have to assign the layer to one of the game objects.当你创建了一个新的层后,必须把它和一个游戏对象关联起来。In the tag manager we assigned the Player layer to be in layer 8.在标签管理器中,把玩家层放到层8中。
Drawing only a part of the scene with the camera's culling mask
使用相机剔除掩膜来对场景部分绘制Using the camera's culling mask, you can selectively render objects which are in one particular layer. To do this, select the camera that should selectively render objects.通过相机剔除掩膜,你可以有选择性地渲染那些在特定层中的物体。为了达到这一目的,首先选择目标相机。Modify the culling mask by checking or unchecking layers in the culling mask property.设置剔除掩膜,只需要在剔除掩膜属性中选择或删除相应层。Casting Rays Selectively 选择性地投射光线Using layers you can cast rays and ignore colliders in specific layers. For example you might want to cast a ray only against the player layer and ignore all other colliders.通过层你可以在特定的层里投射光线、忽略碰撞器。比如说,你只想在玩家层中投射光线(即光线只跟玩家层中的碰撞体作用),进而忽略其他所有的碰撞器。The Physics.Raycast function takes a bitmask, where each bit determines if a layer will be ignored or not. If all bits in the layerMask are on, we will collide against all colliders. If the layerMask = 0, we will never find any collisions with the ray.Physics.Raycast函数里操作一个掩膜位,该掩膜位的每一位决定相应的层是否被忽略。如果layerMask中的每一位都为1,则将会与所有的碰撞器作用。相反如果layerMask=0,那么将会光线将不会与任何碰撞器作用。// bit shift the index of the layer to get a bit mask
var 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))
    print ("The ray hit the player"); In the real world you want to do the inverse of that however. We want to cast a ray against all colliders except those in the Player layer.在现实生活中,会遇到完全相反的情况。如我们只想排除玩家层(Player layer),即投射出的光线跟除玩家层中的碰撞器外的所有碰撞器作用。function Update () {
  // Bit shift the index of the layer (8) to get a bit mask
  var 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;  var hit : RaycastHit;
  // Does the ray intersect any objects excluding the player layer
  if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask)) {
    Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.yellow);
    print ("Did Hit");
  } else {
    Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) *1000, Color.white);
    print ("Did not Hit");
  }
}When you don&#39;t pass a layerMask to the Raycast function, it will only ignore colliders that use the IgnoreRaycast layer. This is the easiest way to ignore some colliders when casting a ray.当没有给Raycast函数传layerMask参数时,仅仅只忽略使用IgnoreRaycast层的碰撞器。这是投射光线时忽略某些碰撞器的最简单方法。【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-30 03:36 , Processed in 0.089570 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部