Unity 2D动画仅在向右移动时播放

问题描述 投票:-1回答:1

这是我第一次实现团结一致。我正在使用一个名为“ isRunning”的布尔值,该布尔值在每个用于检测输入的if语句下均设置为true。应该触发2D骑士角色的动画运行。但是,这仅适用于“ d”键,不适用于其他任何键(wa和s)。这是代码:

private void TakeInput()
    {
        direction = Vector2.zero;

        if (Input.GetKey(KeyCode.W))
        {
            direction += Vector2.up;
            animator.SetBool("isRunning", true);
        }
        if (Input.GetKey(KeyCode.A))
        {
            direction += Vector2.left;
            animator.SetBool("isRunning", true);
        }
        if (Input.GetKey(KeyCode.S))
        {
            direction += Vector2.down;
            animator.SetBool("isRunning", true);
        }
        if (Input.GetKey(KeyCode.D))
        {
            direction += Vector2.right;
            animator.SetBool("isRunning", true);
        }
        else 
        {
            animator.SetBool("isRunning", false);
        }
    }

enter image description here

c# unity3d animation sprite
1个回答
0
投票

the else仅连接到最后一个if。如果在某些情况下应该是其他情况,例如]

{
    direction = Vector2.zero;

    if (Input.GetKey(KeyCode.W))
    {
        direction += Vector2.up;
        animator.SetBool("isRunning", true);
    }
    else if (Input.GetKey(KeyCode.A))
    {
        direction += Vector2.left;
        animator.SetBool("isRunning", true);
    }
    else if (Input.GetKey(KeyCode.S))
    {
        direction += Vector2.down;
        animator.SetBool("isRunning", true);
    }
    else if (Input.GetKey(KeyCode.D))
    {
        direction += Vector2.right;
        animator.SetBool("isRunning", true);
    }
    else 
    {
        animator.SetBool("isRunning", false);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.