简体   繁体   中英

Unity 2D animation only playing when moving right

This is my first time animating in unity. I am using a boolean titled "isRunning" which is set to true under each if statement for detecting input. This is supposed to trigger the 2D knight character's animation to run. However, this only works for the "d" key and not for any of the others (wa and s). Here is the code:

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

在此处输入图像描述

The else is only connected to the last if. It should be else if on some of the cases, like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM