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

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. (转) 一致性Hash算法在Memcached中的应用

    前言 大家应该都知道Memcached要想实现分布式只能在客户端来完成,目前比较流行的是通过一致性hash算法来实现.常规的方法是将 server的hash值与server的总台数进行求余,即hash ...

  2. python爬虫错误

    错误描述 TypeError: list indices must be integers or slices, not str 错误缘由 取标签属性的时候, find_all()函数与find()函 ...

  3. ubuntu遇到的问题

    昨天分辨率在装一个游戏时被更改了,改过之后,怎么都改不过来... 折腾了一下午,在配置文件中/etc/X11/xorg.conf.failsafe,但是网上都是/etc/X11/xorg.conf, ...

  4. BZOJ3211: 花神游历各国(线段树)

    3211: 花神游历各国 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 5692  Solved: 2114[Submit][Status][Discu ...

  5. Loj 2005 相关分析

    Loj 2005 相关分析 大力把式子拆开. \[ \begin{aligned} a &= \frac {\sum_{i=L}^{R} (x_i-\bar{x})(y_i-\bar{y})} ...

  6. C# 4.0中的动态类型和动态编程

    # 4.0的主题就是动态编程(Dynamic Programming).虽然C#仍然是一种静态语言,但是对象的意义开始变得越来越“动态”.它们的结构和行为无法通过静态类型来捕获,或者至少编译器在编译程 ...

  7. streamsets stream selector 使用

    stream selector 就是一个选择器,可以方便的对于不同record 的数据进行区分,并执行不同的处理 pipeline flow stream selector 配置 local fs 配 ...

  8. ambassador 学习三 限速处理

    与认证类似ambassador 也是委托给三方的其他服务进行限速处理 基本的环境安装可以参考相关文档,主要还是qotm 服务 官方参考实现的简单限速服务 --- apiVersion: v1 kind ...

  9. oracle undo表空间大小修改

    redhat:清空回收站 rm -rf  /home/登录用户名/.Trash 例子:rm -rf /home/.Trash-root df命令可以显示目前所有文件系统的可用空间及使用情形: 例子:d ...

  10. SQL Server中的事务与其隔离级别之脏读, 未提交读,不可重复读和幻读

    原本打算写有关 SSIS Package 中的事务控制过程的,但是发现很多基本的概念还是需要有 SQL Server 事务和事务的隔离级别做基础铺垫.所以花了点时间,把 SQL Server 数据库中 ...