这次的代码示例是配合动画系统使用的

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的更多相关文章

  1. Unity3D料槽设备旋转(一)

    1.使用C#创建控制游戏对象的的脚本语言, 第一步: 在project师徒中create 一个C#脚本,将其按照自己的设备名称进行命名,这里我将其简单的命名成zhuaquanzhou.cs 使用编辑器 ...

  2. unity3d 移动与旋转 1

    移动与旋转 1 player角色随asdw按键左右上下移动并旋转 public void Update() { // Reset player rotation to look in the same ...

  3. Unity3D 中 用quaternion 来对一个坐标点进行旋转的初步体会

    在unity3d中,用四元数来表示旋转,四元数英文名叫quaternion . 比如 transform.rotation 就是一个四元数,其由四个部分组成 Quaternion = (xi + yj ...

  4. 09应用输入经理旋转场景--《猿学校课程Unity3d》

    为什么极品飞车游戏等.,我们可以通过系统设置非常的方面根据自己喜欢的操作模式设置,有些人喜欢用箭头来控制不喜欢与使用"W,S,A,D"控制,这就解释程序猿不会死在程序写入内部控制, ...

  5. Unity3D 如何图形问题修正旋转模型已导入?

     如何纠正旋转模型被导入? 一些立体艺术资源包导出其模式,以便 Z 轴向上.Unity 大多数标准的脚本中假定的三维世界 Y 轴代表了.在 Unity 比改动脚本使其契合easy得多. Z 轴朝上 ...

  6. 【坦克大战】Unity3D多人在线游戏(泰课的坦克大战--旋转的螺丝钉)

    [坦克大战]Unity3D多人在线游戏 http://www.taikr.com/my/course/937 1.NetworkManager的介绍: 说明:选择固定生成时会自动寻找有StartPos ...

  7. 【转载】Unity3D研究院之IOS触摸屏手势控制镜头旋转与缩放

    前几篇文章介绍了很多Unity3D引擎自身的一些问题, 今天我们在回到IOS设备上讨论一些触摸屏幕手势,本章的目标是通过触摸iPhone屏幕手势 实现模型左右的旋转,与模型的缩放. 大家想一想模型的旋 ...

  8. 【转】Unity3D研究院之设置自动旋转屏幕默认旋转方向

    http://www.xuanyusong.com/archives/2871 如下图所示,在处理屏幕默认旋转方向的时候可以在这里进行选择,上下左右一共是4个方向. 策划的需求是游戏采用横屏,但是要求 ...

  9. unity3d 触屏多点触控(旋转与缩放)

    unity3d 触屏多点触控(旋转与缩放) /*Touch OrbitProgrammed by: Randal J. Phillips (Caliber Mengsk)Original Creati ...

随机推荐

  1. Javascript中的prototype与继承

    通常来说,javascript中的对象就是一个指向prototype的指针和一个自身的属性列表.javascript创建对象时采用了写时复制的理念. 只有构造器才具有prototype属性,原型链继承 ...

  2. [爬虫] 学Scrapy,顺便把它的官方教程给爬下来

    想学爬虫主要是因为算法和数据是密切相关的,有数据之后可以玩更多有意思的事情,数据量大可以挖掘挖掘到更多的信息. 之前只会通过python中的request库来下载网页内容,再用BeautifulSou ...

  3. UITableView-(单元格的自定义方法)

    //contentView //行内容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS ...

  4. Bakery

    Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. The ...

  5. Cannot find name 'AsyncIterator' error in Typescript compilation process 问题解决

    解决方法: tsconfig.json: 添加lib 编译选项 { "compilerOptions": { "lib":[ "esnext.asyn ...

  6. BZOJ1252:序列终结者

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...

  7. Spring MVC @ResponseBody和@RequestBody使用

    @ResponseBody用法: 作用:该注解用于将Controller的方法返回的对象,根据HTTP Request Header的Accept的内容,通过适当的HttpMessageConvert ...

  8. Java 经典练习题_Day010

    final 变量能被显式地初始化并且只能初始化一次.被声明为 final 的对象的引用不能指向不同的对象.但是 final 对象里的数据可以被改变.也就是说 final 对象的引用不能改变,但是里面的 ...

  9. PMON和SMON的功能

    PMON:进程监控进程进程负责在反常中断的连接之后的清理工作.例如,如果因某些原因专用服务“故障”或被kill掉,PMON就是负责处理(恢复或回滚工作)和释放你的资源.PMON将发出未提交工作的回滚, ...

  10. 软件测试——等价类划分(EditText * 3)

    一.程序要求 EditBox 同时允许输入三个1到6个英文字符或数字,点击确定结束. 二.测试分析 编号 第一个输入框 第二个输入框 第三个输入框 输出 1 null null null 三个输入框均 ...