解决Unity 状态机Animator鬼畜动画。

状态机的基础知识这里有位老哥讲的非常好https://blog.csdn.net/qq_34134078/article/details/53092653

其实很简单,在逻辑上重置状态机就可以,看代码。

    private float v = 0;
    private float h = 0;

void Start () {//第一个输入储存
        v = Input.GetAxisRaw("Vertical");
        if (v != 0)
            return;
        h = Input.GetAxisRaw("Horizontal");
        if (h != 0)
            return;
    }

private void FixedUpdate()

    {

//重置状态机状态

        if (h != 0)
            GetComponent<Animator>().SetFloat("DirX", 0);
        if (v != 0)
            GetComponent<Animator>().SetFloat("DirY", 0);
        h = Input.GetAxisRaw("Horizontal");
        transform.Translate(Vector2.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);
        if (h > 0)
        {
            GetComponent<Animator>().SetFloat("DirX", dir * h);
        }
        else if (h < 0)
        {
            GetComponent<Animator>().SetFloat("DirX", dir * h);
        }
        v = Input.GetAxisRaw("Vertical");
        transform.Translate(Vector2.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);
        if (v > 0)
        {
            GetComponent<Animator>().SetFloat("DirY", dir * v);
        }
        else if (v < 0)
        {
            GetComponent<Animator>().SetFloat("DirY", dir * v);
        }

    }

其实也可以在中间判定h!=0直接return;可是那样的话,操作的时候总有一种不爽快的感觉,大家可以自己试验一下。

猜你喜欢

转载自blog.csdn.net/qq_15020543/article/details/80677282