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

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);
}
}

最新文章

  1. Java中Comparable与Comparator的区别
  2. rsa密钥文件转化为tortoise认可的pak密钥文件
  3. js中的块作用域
  4. OFDM学习之旅
  5. oracle kill session
  6. CLR 公共语言运行库
  7. 【JDBC】向数据表插入数据时,自动获取生成的主键
  8. 【转载】Android通过ksoap2调用.net(c#)的webservice
  9. 540C: Ice Cave
  10. hdu 4815 Little Tiger vs. Deep Monkey(01背包)
  11. Delphi7使用ADO直接连接Excel读取数据
  12. hdu1023:卡特兰数
  13. html5本地存储web storage的简单使用
  14. 卡尔曼滤波法C编程
  15. 大数据 - hadoop - HDFS+Zookeeper实现高可用
  16. Appium+python+html生成饼图测试报告
  17. 【十二】jvm 性能调优工具之 jhat (JVM Heap Analysis Tool)
  18. ubuntu终端命令启动matlab方法
  19. node微信公众号开发---域名绑定
  20. MAC系统下用Idea创建spring boot工程 基于maven

热门文章

  1. IE9(8)跨域(CORS)解决方案
  2. Android listview自定义分割线宽度
  3. java程序运行时内存分配详解
  4. 【转】inittab文件
  5. oracle之 Got minus one from a read call 与 ORA-27154: post/wait create failed
  6. php 自定义函数大全
  7. C#:进程、线程、应用程序域(AppDomain)与上下文分析
  8. 使用正则表达式去除html标签
  9. java web jsp
  10. debian修改ssh端口