[Unity2D]Tags和Layers
Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject。添加Tags和Layers的操作如下:
"Edit" -> "Project Settings" -> "Tags and Layers"来打开设置面板。
tag可以理解为一类元素的标记,如hero、enemy、apple-tree等。通过设置tag,可以方便地通过GameObject.FindWithTag()来寻找对象。GameObject.FindWithTag()只返回一个对象,要想获取多个tag为某值的对象,GameObject.FindGameObjectsWithTag()。
每个GameObject的Inspector面板最上方都也有个Layer选项,就在Tag旁边,unity已经有了几个层,我们新建个层,也叫UI,点击Add Layer,可以看到从Layer0到Layer7都灰掉了,那是不能用的,从第八个起可以用,Layer和tag还有一个很大的区别就是layer最多只能有32个层。
在使用过程中,使用Culling Mask、Layer Mask的地方实际指的就是layer。下面我们来看一段使用layer来检测碰撞的代码:
Ray ray1 = nguiCamera.camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit1;
LayerMask mask = << LayerMask.NameToLayer("UI");
if (Physics.Raycast(ray1, out hit1, , mask.value))
{
return;
}
LayerMask的NameToLayer是通过层的名称返回该层的索引,如果是8,然后1<<8换算成LayerMask值,再用LayerMask的value就可以了。注意也必须设置collider才能接收碰撞,这里才能判断到。LayerMask实际上是一个位码操作,在Unity中Layers一共有32层,这个是不能增加或者减少的: 1 << LayerMask.NameToLayer("UI") 这一句实际上表示射线查询只在UI所在这个层级查找是返回的该名字所定义的层的层索引,注意是从0开始。
下面我们来看一个玩家控制的脚本,利用 Physics2D.Linecast检测物体是否碰撞到地面。
using UnityEngine;
using System.Collections; public class PlayerControl : MonoBehaviour
{
[HideInInspector]
public bool facingRight = true; // For determining which way the player is currently facing.
[HideInInspector]
public bool jump = false; // Condition for whether the player should jump. public float moveForce = 365f; // Amount of force added to move the player left and right.
public float maxSpeed = 5f; // The fastest the player can travel in the x axis.
public AudioClip[] jumpClips; // Array of clips for when the player jumps.
public float jumpForce = 1000f; // Amount of force added when the player jumps.
public AudioClip[] taunts; // Array of clips for when the player taunts.
public float tauntProbability = 50f; // Chance of a taunt happening.
public float tauntDelay = 1f; // Delay for when the taunt should happen. private int tauntIndex; // The index of the taunts array indicating the most recent taunt.
private Transform groundCheck; // A position marking where to check if the player is grounded.
private bool grounded = false; // Whether or not the player is grounded.
private Animator anim; // Reference to the player's animator component. void Awake()
{
// Setting up references.
// 在子对象里面找到groundCheck
groundCheck = transform.Find("groundCheck");
// 获取当前的动画控制器
anim = GetComponent<Animator>();
} void Update()
{
// 是为能随时检测到groundCheck这个物体,添加一个名叫Ground的Layer,然后把场景中的所有代表地面的物体的Layer设为Ground
// 这里用到了2D射线检测Physics2D.Linecast()
// LayerMask实际上是一个位码操作,在Unity3d中Layers一共有32层,这个是不能增加或者减少的:
// 1 << LayerMask.NameToLayer("Ground") 这一句实际上表示射线查询只在Ground所在这个层级查找 是返回的该名字所定义的层的层索引,注意是从0开始
// 每个GameObject的Inspector面板最上方都也有个Layer选项,就在Tag旁边,unity3d已经有了几个层,我们新建个层,也叫UI,点击Add Layer,可以看到从Layer0到Layer7都灰掉了,那是不能用的,从第八个起可以用,所以在第八个建个UI的层。
// 一般情况下我们只用前两个参数,distance表示射线距离,默认是无限远,重点是最后一个参数layerMask,专门处理layer过滤的,是个整型,怎么用呢,是靠layer的二进制位来操作的
// LayerMask的NameToLayer是通过层的名称返回该层的索引,这里是8,然后1<<8换算成LayerMask值,再用LayerMask的value就可以了。
// 注意也必须设置collider才能接收碰撞,这里才能判断到。
grounded = Physics2D.Linecast(transform.position, groundCheck.position, << LayerMask.NameToLayer("Ground")); // If the jump button is pressed and the player is grounded then the player should jump.
// 如果点击了跳的按键,并且已经着陆,那么就可以跳起来
if(Input.GetButtonDown("Jump") && grounded)
jump = true;
} // 因为主角游戏对象要使用到刚体力,所以一定要写在FixedUpdate里面,不能放在Update上
void FixedUpdate ()
{
// Cache the horizontal input.
// 换取水平方向的移动距离
float h = Input.GetAxis("Horizontal"); // The Speed animator parameter is set to the absolute value of the horizontal input.
// 设置动画的速度变量
anim.SetFloat("Speed", Mathf.Abs(h)); // 给物体添加一个水平的力,让它移动的时候会产生惯性的效果
// If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
// 如果速度小于最大的速度
if(h * rigidbody2D.velocity.x < maxSpeed)
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce); // If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y); // 转身
// If the input is moving the player right and the player is facing left...
if(h > && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < && facingRight)
// ... flip the player.
Flip(); // If the player should jump...
if(jump)
{
// Set the Jump animator trigger parameter.
// 触发跳的动画
anim.SetTrigger("Jump"); // Play a random jump audio clip.
int i = Random.Range(, jumpClips.Length);
AudioSource.PlayClipAtPoint(jumpClips[i], transform.position); // Add a vertical force to the player.
// 添加一个垂直的力
rigidbody2D.AddForce(new Vector2(0f, jumpForce)); // Make sure the player can't jump again until the jump conditions from Update are satisfied.
jump = false;
}
} // 转身
void Flip ()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight; // Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -;
transform.localScale = theScale;
} public IEnumerator Taunt()
{
// Check the random chance of taunting.
float tauntChance = Random.Range(0f, 100f);
if(tauntChance > tauntProbability)
{
// Wait for tauntDelay number of seconds.
yield return new WaitForSeconds(tauntDelay); // If there is no clip currently playing.
if(!audio.isPlaying)
{
// Choose a random, but different taunt.
tauntIndex = TauntRandom(); // Play the new taunt.
audio.clip = taunts[tauntIndex];
audio.Play();
}
}
} int TauntRandom()
{
// Choose a random index of the taunts array.
int i = Random.Range(, taunts.Length); // If it's the same as the previous taunt...
if(i == tauntIndex)
// ... try another random taunt.
return TauntRandom();
else
// Otherwise return this index.
return i;
}
}
[Unity2D]Tags和Layers的更多相关文章
- 【Unity3D游戏开发】基础知识之Tags和Layers (三二)[转]
Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject.添 ...
- Tags and Layers
[Tags and Layers] 1.tags and layers 配置面板."Edit" -> "Project Settings" -> & ...
- 【Unity3D】Tags和Layers
Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject.添 ...
- Unity3D-第一视角射击游戏
一.新建关卡 File,Save Scene,File,New Scene,File,Save Scene as... ,Level02.unity 1.建立场景 从Assets中拖放场景模型到Hie ...
- Unity3D Layer要点
简介 Layer可以用于光照的分层和物理碰撞的分层,这样可以很好地进行性能优化 数据结构 Layer在Unity中有3中呈现方式:1.string名字,2.int层索引 ...
- 在Unity中实现小地图(Minimap)
小地图的基本概念众所周知,小地图(或雷达)是用于显示周围环境信息的.首先,小地图是以主角为中心的.其次,小地图上应该用图标来代替真实的人物模型,因为小地图通常很小,玩家可能无法看清真实的模型.大多数小 ...
- Unity 2D游戏开发教程之游戏中精灵的跳跃状态
Unity 2D游戏开发教程之游戏中精灵的跳跃状态 精灵的跳跃状态 为了让游戏中的精灵有更大的活动范围,上一节为游戏场景添加了多个地面,于是精灵可以从高的地面移动到低的地面处,如图2-14所示.但是却 ...
- UNITY VR 视频/图片 开发心得(二)
上回说到了普通的全景图片,这回讲真正的VR. 由于这种图片分为两部分,所以我们需要两个Camera对象以及两个球体.首先新建一个Camera对象,并将其命名为RightEye(其它名字也无妨,只要你自 ...
- Unity射击游戏实例—物理碰撞的实现
前言: 这一篇章实现物理碰撞,就是游戏体碰撞减装甲,这几天想要试着做出兼具装甲与血量的模式,可自动回复的装甲与永久损伤的血量,在一些平台上找到了不少有意思的模型,有兴趣的可以自己找找模型替换一下. 射 ...
随机推荐
- PAT乙级 1014. 福尔摩斯的约会 (20)
1014. 福尔摩斯的约会 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 大侦探福尔摩斯接到一张奇怪的 ...
- ThinkPHP讲解(七)——修改删除
修改数据 方式一:数组方式,直接将数据库里需要修改的内容进行修改 function Update() { //1.数组方式 $model=D("Info"); $attr=arra ...
- ANT命令总结(转载)
1 Ant是什么? Apache Ant 是一个基于 Java的生成工具.生成工具在软件开发中用来将源代码和其他输入文件转换为可执行文件的形式(也有可能转换为可安装的产品映像形式).随着应用程序的生成 ...
- DataGridView 些许事件测试
原始设计需求:当单元格内容是空白时,鼠标进入之后,显示一些数据 直观的第一感觉必然是用CellClick,细想,如果用户不用鼠标,直接按Tab键切换单元格呢?又或者,用户直接双击涅~ 主要测试的是: ...
- Swift常量和变量
常量和变量由一个特定名称来表示,如maxNumber 或者 message.常量所指向的是一个特定类型的值, 如数字10或者字符”hello”.变量的值可以根据需要不断修改,而常量的值是不能够被二次修 ...
- Junit单步调试
单步调试:主要查看变量内容的变化 1.设置断点位置,设置在可能出现问题的代码 2.点击项目右键以Debug as方式运行程序 3.F5 --> step into 进入方法内部进行调试 ...
- 使用QTP对Flight的登录界面进行测试
一.测试用例设计 现在使用QTP对案例程序进行测试, 设计测试用例的要求为: 用户名长度大于等于6个字符 必须为字母[o-z,O-Z]和数字[0-9]组成 不能为空,空格或者特殊字符 正确的密码为:M ...
- Intent跳转传list集合
先把List<>改为ArrayList<> ArrayList<Good> list=new ArrayList<Good>(); Intent int ...
- javax.transaction.xa.XAException: java.sql.SQLException: 无法创建 XA 控制连接。(SQL 2000,SQL2005,SQL2008)
javax.transaction.xa.XAException: java.sql.SQLException:无法创建 XA 控制连接.错误: 未能找到存储过程'master..xp_sqljdbc ...
- java运算符优先级记忆口诀
尊重原创:(口诀)转自http://lasombra.iteye.com/blog/991662 今天看到<java编程思想>中的运算符优先级助记口诀,不过"Ulcer Addi ...