博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unity3d 移动与旋转 2
阅读量:4965 次
发布时间:2019-06-12

本文共 3502 字,大约阅读时间需要 11 分钟。

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

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
(); hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent
(); // Set the weight of the shouting layer to 1. anim.SetLayerWeight(1, 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, 0); } 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(0).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); }}

 

转载于:https://www.cnblogs.com/88999660/p/3710577.html

你可能感兴趣的文章
提示系统启动关于误更改/var下诺干的权限问题,导致系统启动提示The System is running in low-graphics mode问题解决 By ACReaper...
查看>>
添加设置Android编程心得-为TextView添加各种样式
查看>>
[Oracle] Data Pump 详细使用教程(1)- 总览
查看>>
Install windows server 2008 on ESXi 5.1, add to domain and config for remote desktop
查看>>
nullnullupdate linux user or root password
查看>>
安装文件Win7 配置 Nutch 1.2
查看>>
绑定域名到JavaWeb项目,由域名直接访问到网站首页
查看>>
移动端重构实战系列3——各种等分
查看>>
产品应该努力提高用户使用的方便性
查看>>
React 附件动画API ReactCSSTransitionGroup
查看>>
Graph cut使用方法
查看>>
C#中静态与非静态方法比较(转)
查看>>
排序算法(四)选择排序
查看>>
redis连接数据库
查看>>
ubuntu频繁死机--独立显卡问题
查看>>
网络流---最大流(Edmond-Karp算法)的学习
查看>>
jvm(13)-线程安全与锁优化
查看>>
网络流24题——太空飞行计划问题(最大权闭合图)
查看>>
HTTP状态码
查看>>
RunLoop(运行循环)-002-加载大图
查看>>