【Unity3D游戏开发】基础知识之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;
}
}
原文链接:http://www.cnblogs.com/linzheng/p/3991375.html
【Unity3D游戏开发】基础知识之Tags和Layers (三二)[转]的更多相关文章
- Html5 Canvas核心技术(图形,动画,游戏开发)--基础知识
基础知识 canvas 元素可以说是HTML5元素中最强大的一个,他真正的能力是通过canvas的context对象表现出来的.该环境对象可以从canvas元素身上获得. <body> & ...
- Unity3D游戏开发初探—2.初步了解3D模型基础
一.什么是3D模型? 1.1 3D模型概述 简而言之,3D模型就是三维的.立体的模型,D是英文Dimensions的缩写. 3D模型也可以说是用3Ds MAX建造的立体模型,包括各种建筑.人物.植被. ...
- [Unity3D]Unity3D游戏开发《反对》说到游戏(上)——目标跟踪
朋友,大家好.我是秦培,欢迎关注我的博客.我的博客地址blog.csdn.net/qinyuanpei. 首先博主要自我反省,过了这么久才来更新博客,这段时间主要是在忙着写期末的作业,所以博主基本上没 ...
- [Unity3D]Unity3D游戏开发3D选择场景中的对象,并显示轮廓效果强化版
大家好,我是秦培,欢迎关注我的博客,我的博客地址blog.csdn.net/qinyuanpei. 在上一篇文章中,我们通过自己定义着色器实现了一个简单的在3D游戏中选取.显示物体轮廓的实例. 在文章 ...
- 从一点儿不会开始——Unity3D游戏开发学习(一)
一些废话 我是一个windows phone.windows 8的忠实粉丝,也是一个开发者,开发数个windows phone应用和两个windows 8应用.对开发游戏一直抱有强烈兴趣和愿望,但奈何 ...
- RPG游戏开发基础教程
RPG游戏开发基础教程 第一步 下载RPG Maker 开发工具包 1.RPG Maker 是什么? RPG Maker 是由Enterbrain公司推出的RPG制作工具. 中文译名为RPG制作大师. ...
- [Unity3D]Unity3D游戏开发之异步记载场景并实现进度条读取效果
大家好,我是秦元培.欢迎大家关注我的博客,我的博客地址是:blog.csdn.net/qinyuanpei.终于在各种无语的论文作业中解脱了,所以立即抓紧时间来这里更新博客.博主本来计划在Unity3 ...
- 3D开发基础知识和简单示例
引言 现在物联网概念这么火,如果监控的信息能够实时在手机的客服端中以3D形式展示给我们,那种体验大家可以发挥自己的想象. 那生活中我们还有很多地方用到这些,如上图所示的Kinect 在医疗上的应用,当 ...
- Unity3D游戏开发和网络游戏实战书籍及配套资源和一些视频教程分享
目录 1. 按 2. pdf 3. 配套资源 3.1. Unity网络游戏实战第二版 3.2. Unity网络游戏实战第一版 4. 视频教程 5. 更多坦克大战代码 1. 按 本文主要分享了: Uni ...
随机推荐
- Trace Sys
ARM片上调试和跟踪解决方案(包括CoreSight体系结构,嵌入式跟踪宏单元(ETM),程序流程跟踪(PTM),ARM调试接口(ADI), 跟踪缓冲器(ETB),嵌入式交叉触发器(CTM)) Cor ...
- springmvc下上传文件
使用ajax+表单+jQuery: function sendFile() { var action = "c/goFile.do"; $("#form").a ...
- Java编程思想重点
1. Java中的多态性理解(注意与C++区分) Java中除了static方法和final方法(private方法本质上属于final方法,因为不能被子类访问)之外,其它所有的方法都是动态绑定,这意 ...
- 改Bug总结
[1]屏蔽取舍法 屏蔽取舍,即所谓与问题无关的前后“语境”完全可以忽略,首先屏蔽掉,再根据问题复现路径查看问题发生的区间,然后逐近锁定“病灶”,确定需要修改的目标. [2]追溯原形法 追溯原形,即需要 ...
- :parent 匹配含有子元素或者文本的元素
描述: 查找所有含有子元素或者文本的 td 元素 HTML 代码: <table> <tr><td>Value 1</td><td>< ...
- 如何查看Linux操作系统版本
1. 查看内核版本命令: 360kb.com:~> cat /proc/version Linux version 2.6.32-358.el6.x86_64 (mockbuild@c6b8.b ...
- oracle 自动关闭 数据库连接
数据库启动后过一会自动断开连接(大概10分钟) 方法一.直接修改资源配置文件 分三个步骤在sqlplus环境下完成. 第一步,查询资源文件,找到CONNECT_TIME所在的pr ...
- git 冲突
git中冲突会用特殊的标记 (<<<<<<<=======>>>>>>>) 特殊标记<<<< ...
- linux终端拖动鼠标总是产生ctrl+c
是因为有道词典,打开有道词典设置,取消勾选取词划词里面的所有框框
- DBUtils开源JDBC类库,对JDBC简单封装(作用是:简化编码工作量,同时不会影响程序的性能)
DBUtils:提高了程序的性能,编程更加简便 架包 mysql-connector-java-jar commons-dbcp-1.4jar commons-pool-1.5.5jar common ...