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:

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

Answers


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

    }
}

If two animations are playing at the same time, it means that the condition is used as transition for 2 graphs. For example, your idle_left --> run_left and idle_up --> run_up have certainly the same condition of transition (Up == 1.0 I assume). If you want animations to be independent, they shouldn't have the same condition of transition.

Hope it helps.


Need Your Help

Using value of lower level divs in ng-repeat

angularjs angularjs-ng-repeat ng-show

&lt;div ng-show="level1.collapsed || level2.selected" ng-repeat="level1 in main"&gt; ...