Search code examples
c#unity-game-engine2danimator

Character animator issue animation not working correctly


hi im working on a mini 2d game in unity and i just creat a 2d character and some animation (Up_Idle,Down_Idle,Right_Idle,Left_Idle/Up_run,Down_run,Right_run,Left_run) the problem is these animation are not working correctly (for example when i press upArrow the characte not playing the Up_run animation but he play Up_run and left_run animation in the same time)

here the screenshot:

enter image description here

here is the code:

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour
{

    public float speedX =1f;

    public float speedY =1f;

    Animator animator;

    void Start()
    {
        animator = GetComponent <Animator> ();
    }

    void Update()
    {
        if (Input.GetKey (KeyCode.UpArrow)) {
                    transform.Translate (new Vector2 (speedX, speedY) * Time.deltaTime);
                    animator.SetFloat ("Up", 1);
                    return;
        } else {
            animator.SetFloat ("Up", 0);
        }

        if (Input.GetKey (KeyCode.DownArrow)) {
            transform.Translate (new Vector2 (-speedX, -speedY) * Time.deltaTime);
            animator.SetFloat ("Down", 1);
            return;

        } else {
            animator.SetFloat ("Down", 0);
        }

        if (Input.GetKey (KeyCode.RightArrow)) {
            transform.Translate (new Vector2 (speedX, -speedY) * Time.deltaTime);
            animator.SetFloat ("Right", 1);
            return;
        } else {
            animator.SetFloat ("Right", 0);
        }

        if (Input.GetKey (KeyCode.LeftArrow)) {
            transform.Translate (new Vector2 (-speedX, speedY) * Time.deltaTime);
            animator.SetFloat ("Left", 1);
            return;
        } else {
            animator.SetFloat ("Left", 0);
        }
    } 
}

Solution

  • private Animator animator;

    // Use this for initialization
    void Start()
    {
        animator = this.GetComponent<Animator>();
    
    }
    
    // Update is called once per frame
    void Update()
    {
    
        var vertical = Input.GetAxis("Vertical");
        var horizontal = Input.GetAxis("Horizontal");
    
        if (vertical > 0)
        {
            animator.SetInteger("Direction", 1);
             transform.Translate(Vector2.up* Time.deltaTime);
            //animator.SetInteger("Direction", 2);
            animator.SetFloat("Speed", 1.0f);
        }
        else if (vertical < 0)
        {
            animator.SetInteger("Direction", 3);
            transform.Translate(Vector3.down* Time.deltaTime);
            //animator.SetInteger("Direction", 0);
            animator.SetFloat("Speed", 1.0f);
        }
        else if (horizontal < 0)
        {
            animator.SetInteger("Direction", 4);
    
              transform.Translate(Vector3.left* Time.deltaTime);
    
            //transform.Translate(Vector2.right* Time.deltaTime);
            animator.SetFloat("Speed", 1.0f);
        }
        else if (horizontal > 0)
        {
    
        animator.SetInteger("Direction", 2);
        transform.Translate(Vector2.right* Time.deltaTime);
         // animator.SetInteger("Direction", 4);
        animator.SetFloat("Speed", 1.0f);
        }
        else
        {
          //animator.SetInteger("Direction", 6);
            animator.SetFloat("Speed", 0.0f);
    
        }
    }