Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity 2D动画仅在向右移动时播放_C#_Unity3d_Animation_Sprite - Fatal编程技术网

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

C# Unity 2D动画仅在向右移动时播放,c#,unity3d,animation,sprite,C#,Unity3d,Animation,Sprite,这是我第一次在unity中制作动画。我使用的是一个名为“isRunning”的布尔值,在每个if语句下都设置为true,用于检测输入。这将触发2D骑士角色的动画运行。但是,这仅适用于“d”键,而不适用于任何其他键(w a和s)。 代码如下: private void TakeInput() { direction = Vector2.zero; if (Input.GetKey(KeyCode.W)) { dire

这是我第一次在unity中制作动画。我使用的是一个名为“isRunning”的布尔值,在每个if语句下都设置为true,用于检测输入。这将触发2D骑士角色的动画运行。但是,这仅适用于“d”键,而不适用于任何其他键(w a和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);
        }
    }

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);
    }
}