Unity

xiaoxiao2021-02-28  80

我们需要实现可以向左向前向右跑

混合树创建步骤:1.右击出现creat state -> from blend Tree

配置混合树

参考代码如下:

using UnityEngine; using System.Collections; public class AnimatorTest : MonoBehaviour { private Animator anim; private float horizontal; private float vertical; //旋转的速度 public float rotateSpeed; //向前跑的速度 public float speed; // Use this for initialization void Start () { anim = GetComponent<Animator>(); } // Update is called once per frame void Update () { // horizontal = Input.GetAxis("Horizontal"); vertical = Input.GetAxis("Vertical"); //W键或者上方向键按下的时候 if (Input.GetKey(KeyCode.W)|| Input.GetKey(KeyCode.UpArrow)) { //如果只按下W键或者上方向键按下的时候 anim.SetBool("Run",true); transform.Translate(Vector3.forward * Time.deltaTime * speed); //如果按下W键或者上方向键按下的时候同事按下AD或者左右方向键的时候执行左跑或者右跑的动作 if (vertical != 0) { anim.SetFloat("RunValue", horizontal); transform.Translate(Vector3.forward * Time.deltaTime * speed * vertical); Vector3 direction = Vector3.forward * vertical + Vector3.right * horizontal; transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed * horizontal); } } //W键或者上方向键抬起的时候执行idle动画 if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow)) { anim.SetBool("Run",false); } } }

效果如下:

转载请注明原文地址: https://www.6miu.com/read-59811.html

最新回复(0)