UnityEngine;
using System.Collections; public class PlayerControl : MonoBehaviour
{
[HideInInspector]
public bool facingRight = true; // 为了确定玩家正在面临哪种途径
[HideInInspector]
public bool jump = false; // 判断玩家是否该跳 public float moveForce = 365f; // Amount of force added to move the player left and right.
public float maxSpeed = 5f; // 玩家能到达的最大速度
public AudioClip[] jumpClips; // 玩家跳的时候的声音
public float jumpForce = 1000f; // Amount of force added when the player jumps.
public AudioClip[] 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; // 一个去判断玩家是否接触地面的标志
private bool grounded = false; // Whether or not the player is grounded.
private Animator anim; // 指向玩家的动画组件 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;
}
}

1、AudioClip 接口是用于播放音频剪辑的简单抽象。多个 AudioClip 项能够同时播放,得到的声音混合在一起可产生合成声音

play
void play()开始播放此音频剪辑。每次调用此方法时,剪辑都从头开始重新播放。
loop
void loop()以循环方式开始播放此音频剪辑。
stop
void stop()停止播放此音频剪辑。

2、

unity  input类操作

转载2016-06-07 19:53:03

Input 输入

按键

Input.GetKey(“up”) = Input.GetKey(KeyCode.UpArrow) 按住键盘上键

Input.GetKeyDown (“up”) 按下键盘上键

Input.GetKeyUp (“up”) 放开键盘上键

(KeyCode.UpArrow)为键码

Input.GetButton(“ ”) = Input.GetKey(“ ”) 两种几乎相同(目前没发现差异)

Input.GetButton一样会有分Input.GetButtonDown & Input.GetButtonUp

(“ ”)符号内为按键英文~ 参考Unity→Edit→Project Settings→Input(可新增)

滑鼠用

Input.GetMouseButton(0) 当0键被按住持续侦测(包含down和up各一次)

Input.GetMouseButtonDown(0) 当0键被按下一次

Input.GetMouseButtonUp(0) 当0键放开一次

PS : 0=左 1=中 2=右

获取轴

Input.GetAxis(“ ”) ~ (“ ”)参考Unity→Edit→Project Settings→Input(可新增)

根据坐标轴传回虚拟座标值,取得输入装置输入时值范围-1 ~ 1

例: Input.GetAxis(“Mouse x”) ~ 可取得滑鼠横向移动增量

Android 常用 Input

触碰

Input.touchCount ~ 触碰数量

Input.GetTouch(0) ~ 当第一支手指触碰时

0=第一支1=第二支2=第三支以此类推(触碰到点的侦测数量上限未测不知)

TouchPhase ~ 触碰状态有分五种Began Moved Stationary Ended Canceled

Began按下Moved移动Stationary按住没移动Ended离开

Canceled用于触碰超过5点以上或贴至脸上时取消追踪

Input.GetTouch(0).position 取得第一支手指触碰座标

例 :

Touch 单点移动判断式

if(Input.touchCount == 1 && Input.GetTouch(0).phase==TouchPhase.Moved){执行}

(触碰数量为1个) 和(第一支手指触碰时的状态为移动) 时{执行}

Touch 双点移动判断式

if(Input.touchCount >1){

if(Input.GetTouch(0).phase==TouchPhase.Moved || Input.GetTouch(1).phase==TouchPhase.Moved){ 执行}}

(触碰数量超过1个) 和

(第一支手指触碰时的状态为移动)或着(第二支手指触碰时的状态为移动) 时{执行}

Input.GetTouch(0) 和 Input.GetMouseButton(0)

单点时大致上互通(不确定是否完全相同)

多点上就会出现差异Touch会取手指前后顺序Mouse会取中心点

Unity 脚本<2>的更多相关文章

  1. Unity 脚本的未来发展

    新技术之IL2CPP 最近,我们谈到了Unity 的WebGL . 在那篇文中我们简要谈论到脚本在 WebGL  中的运行的新技术称为"IL2CPP" .然而IL2CPP 所代表的 ...

  2. Unity脚本在层级面板中的执行顺序测试3

    断断续续的写了3篇,以后有时间可以做成一个系列了 前面2篇测试了GameObject的顺序,以及Awake和OnEnable的时机: Unity脚本在层级面板中的执行顺序测试1 http://www. ...

  3. Unity脚本在层级面板中的执行顺序测试4-附加整理

    测试4为一些附加内容,后续的各种tips都加在此. 前几篇测试的链接: Unity脚本在层级面板中的执行顺序测试1 http://www.cnblogs.com/hont/p/4298110.html ...

  4. 5. Unity脚本的执行顺序

    Unity是不支持多线程的,也就是说我们必须要在主线程中操作它,可是Unity可以同时创建很多脚本,并且可以分别绑定在不同的游戏对象身上,他们各自都在执行自己的生命周期感觉像是多线程,并行执行脚本的, ...

  5. 修改Unity脚本模板的方法合计

    作为一个习惯于偷懒的程序,重复性的无聊内容是最让人无奈的事,就比如我们创建Unity脚本之后,需要手动调整生成的新脚本的格式.编码.内容:如果我们要编写的是编辑器或者服务器端脚本,需要修改的内容就会更 ...

  6. unity脚本运行顺序具体的解释

    unity脚本自带函数执行顺序例如以下:将以下脚本挂在随意物体执行就可以得到 Awake ->OnEable-> Start ->-> FixedUpdate-> Upd ...

  7. unity脚本的运行顺序以及单例的实现

    unity引擎把所有脚本先行编译后,在运行的时候一批,一批的函数进行执行. unity脚本自带函数执行顺序如下:将下面脚本挂在任意物体运行即可得到 Awake ->OnEable-> St ...

  8. Unity脚本自动添加注释脚本及排版格式

    Unity脚本自动添加注释脚本及头部注释排版格式 公司开发项目,需要声明版权所有,,,,标注公司名,作者,时间,项目名称及描述等等. 自己总结实现的现成脚本及头部注释排版文本,添加到模版即可. 文件不 ...

  9. Unity 脚本的执行顺序

    在Unity脚本中常用到的函数就是下面这些,他们的顺序也是按照箭头的方向执行的. Awake ->OnEable-> Start -> FixedUpdate-> Update ...

  10. Unity脚本中各函数成员的生命周期

    在学习Unity时,掌握如何编写脚本是必须掌握的一项基本技能.但是关于Unity的游戏脚本中各函数的生命周期是怎样开始和结束的,它们的执行顺序是如何安排的?这一点我们要清楚的了解. 我们知道Unity ...

随机推荐

  1. C++之RAII惯用法

    http://blog.csdn.net/hunter8777/article/details/6327704 C++中的RAII全称是“Resource acquisition is initial ...

  2. 2018.1.4 UML 第三章 用例图

    第三章 用例图 (1)参与者 是指系统以外的需要使用系统或与系统交互的外部实体,吧阔人.设备.外部系统等. (2)参与者之间的关系 泛化关系的含义是参与者的共同行为提取出来表示成通用行为,并描述成超类 ...

  3. CentOS系统下安装Redis

    1. 安装C语言环境 yum install gcc-c++ 2.下载Redis安装包 http://download.redis.io/releases/redis-3.2.9.tar.gz 3.解 ...

  4. [转] 防止js全局变量污染方法总结

    javaScript 可以随意定义保存所有应用资源的全局变量.但全局变量可以削弱程序灵活性,增大了模块之间的耦合性.在多人协作时,如果定义过多的全局变量 有可能造成全局变量冲突,也就是全局变量污染问题 ...

  5. ETL工具--DataX3.0实战

    DataX是一个在异构的数据库/文件系统之间高速交换数据的工具,实现了在任意的数据处理系统(RDBMS/Hdfs/Local filesystem)之间的数据交换,由淘宝数据平台部门完成. DataX ...

  6. C++ 限定名称查找

    限定名称查找规则实际归纳下来很简单,先对::左边的名称进行查找(遵循,限定,无限定),然后在左边查找到的(此时只查找类型名称)名字的作用域内(含内联名称空间件)查找右边出现的名字,查找到即存在(故可以 ...

  7. C++的新特性for-each

    C++实验课要求用for each 循环来实现关联容器 map 的输出,一开始完全萌比.查了好久的资料才整理出下面的: C++11新特性之一就是类似java的for each循环: map<in ...

  8. Java读取各种文件格式内容

    所需的jar包哦也不要太记得了,大家可以搜搜,直接上代码: import java.io.BufferedInputStream; import java.io.File; import java.i ...

  9. 三十、MySQL 处理重复数据

    MySQL 处理重复数据 有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据. 本章节我们将为大家介绍如何防止数据表出现重复数据及如何删 ...

  10. Python中字符串String的基本内置函数与过滤字符模块函数的基本用法

    Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...