unity3d 移动与旋转 2
这次的代码示例是配合动画系统使用的
4.3新的动画系统允许动画带有位置偏移,只需要在Animator组件中勾选Apply Root Motion我们就可以使用它了。
using UnityEngine;
using System.Collections; public class DonePlayerMovement : MonoBehaviour
{
public AudioClip shoutingClip; // Audio clip of the player shouting.
public float turnSmoothing = 15f; // A smoothing value for turning the player.
public float speedDampTime = 0.1f; // The damping for the speed parameter private Animator anim; // Reference to the animator component.
private DoneHashIDs hash; // Reference to the HashIDs. void Awake ()
{
// Setting up the references.
anim = GetComponent<Animator>();
hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneHashIDs>(); // Set the weight of the shouting layer to 1.
anim.SetLayerWeight(, 1f);
} void FixedUpdate ()
{
// Cache the inputs.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool sneak = Input.GetButton("Sneak"); MovementManagement(h, v, sneak);
} void Update ()
{
// Cache the attention attracting input.
bool shout = Input.GetButtonDown("Attract"); // Set the animator shouting parameter.
anim.SetBool(hash.shoutingBool, shout); AudioManagement(shout);
} void MovementManagement (float horizontal, float vertical, bool sneaking)
{
// Set the sneaking parameter to the sneak input.
anim.SetBool(hash.sneakingBool, sneaking); // If there is some axis input...
if(horizontal != 0f || vertical != 0f)
{
// ... set the players rotation and set the speed parameter to 5.5f.
Rotating(horizontal, vertical);
anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
}
else
// Otherwise set the speed parameter to 0.
anim.SetFloat(hash.speedFloat, );
} void Rotating (float horizontal, float vertical)
{
// Create a new vector of the horizontal and vertical inputs.
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical); // Create a rotation based on this new vector assuming that up is the global y axis.
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up); // Create a rotation that is an increment closer to the target rotation from the player's rotation.
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime); // Change the players rotation to this new rotation.
rigidbody.MoveRotation(newRotation);
} void AudioManagement (bool shout)
{
// If the player is currently in the run state...
if(anim.GetCurrentAnimatorStateInfo().nameHash == hash.locomotionState)
{
// ... and if the footsteps are not playing...
if(!audio.isPlaying)
// ... play them.
audio.Play();
}
else
// Otherwise stop the footsteps.
audio.Stop(); // If the shout input has been pressed...
if(shout)
// ... play the shouting clip where we are.
AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
}
}
unity3d 移动与旋转 2的更多相关文章
- Unity3D料槽设备旋转(一)
1.使用C#创建控制游戏对象的的脚本语言, 第一步: 在project师徒中create 一个C#脚本,将其按照自己的设备名称进行命名,这里我将其简单的命名成zhuaquanzhou.cs 使用编辑器 ...
- unity3d 移动与旋转 1
移动与旋转 1 player角色随asdw按键左右上下移动并旋转 public void Update() { // Reset player rotation to look in the same ...
- Unity3D 中 用quaternion 来对一个坐标点进行旋转的初步体会
在unity3d中,用四元数来表示旋转,四元数英文名叫quaternion . 比如 transform.rotation 就是一个四元数,其由四个部分组成 Quaternion = (xi + yj ...
- 09应用输入经理旋转场景--《猿学校课程Unity3d》
为什么极品飞车游戏等.,我们可以通过系统设置非常的方面根据自己喜欢的操作模式设置,有些人喜欢用箭头来控制不喜欢与使用"W,S,A,D"控制,这就解释程序猿不会死在程序写入内部控制, ...
- Unity3D 如何图形问题修正旋转模型已导入?
如何纠正旋转模型被导入? 一些立体艺术资源包导出其模式,以便 Z 轴向上.Unity 大多数标准的脚本中假定的三维世界 Y 轴代表了.在 Unity 比改动脚本使其契合easy得多. Z 轴朝上 ...
- 【坦克大战】Unity3D多人在线游戏(泰课的坦克大战--旋转的螺丝钉)
[坦克大战]Unity3D多人在线游戏 http://www.taikr.com/my/course/937 1.NetworkManager的介绍: 说明:选择固定生成时会自动寻找有StartPos ...
- 【转载】Unity3D研究院之IOS触摸屏手势控制镜头旋转与缩放
前几篇文章介绍了很多Unity3D引擎自身的一些问题, 今天我们在回到IOS设备上讨论一些触摸屏幕手势,本章的目标是通过触摸iPhone屏幕手势 实现模型左右的旋转,与模型的缩放. 大家想一想模型的旋 ...
- 【转】Unity3D研究院之设置自动旋转屏幕默认旋转方向
http://www.xuanyusong.com/archives/2871 如下图所示,在处理屏幕默认旋转方向的时候可以在这里进行选择,上下左右一共是4个方向. 策划的需求是游戏采用横屏,但是要求 ...
- unity3d 触屏多点触控(旋转与缩放)
unity3d 触屏多点触控(旋转与缩放) /*Touch OrbitProgrammed by: Randal J. Phillips (Caliber Mengsk)Original Creati ...
随机推荐
- QT Creator快捷键不能用
我现在搞明白了,热键之所以不行,是因为我开了Fakevim原因.关了fakevim就能用热键了. 如果开了Fakevim,连基本的Ctrl+C,这样的复制快捷键都不能用. 快速添加方法实体(.cpp) ...
- YAML 与 front-matter
1. YAML 类似 Linux:Linux is not UniX,YAML:YAML ain't markup language,是一种递归缩写,是一个可读性高并且容易被人类阅读,容易和脚本语言交 ...
- Python itertools.combinations 和 itertools.permutations 等价代码实现
最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...
- office 2013母版保存并调用
如果觉得某个ppt的母版不错,想保存下来以后使用的话,那么执行 开始->另存为-> 选择位置和格式,注意格式选择potx. 之后如果想要使用这组母版,怎么办呢? 浏览主题,打开之前保存的 ...
- 转-JAVA webservice之CXF 范例--http://cxshun.iteye.com/blog/1275408
JAVA webservice之CXF 博客分类: j2ee相关 昨天我们一起学习了一下xfire,今天我们来看一下CXF,为什么学完那个接着学这个呢.因为CXF是在xfire的基础上实现 的,所以我 ...
- Erlang ets -- something about cache continue
上一次说到了实现一个简单cache 的基本思路和想法, http://www.cnblogs.com/--00/p/erlang_ets_something_about_cache.html 在文末, ...
- [Java.web]JSTL 使用
<%@ page import="cn.itcast.domain.Person"%> <%@ page language="java" im ...
- 机房用ROS创建时间服务器
发现机房里的服务器时间老是不同步,虽然都设置为time-a.nist.gov和time-b.nist.gov,仍然有失败的概率.可能是因为国外服务器的缘故.所以打算在机房里创建一个时间服务器.正好RO ...
- PHP外部调用网站百度统计数据的方法详解
目的:外部调用网站的百度统计(tongji.baidu.com)数据. 条件:1.具备调用目标网站的百度统计平台管理权限 2.PHP环境支持curl函数. 原理:同PHP小偷程序原理,通过curl函数 ...
- MTU&MSS
MTU是Maximum Transmission Unit的缩写,意为最大传输单元,通俗的理解就是在网络上传送的最大数据包,单位是字节. 以太网对数据帧的长度都有一个限制,其最大值为1500,这个特性 ...