[C#] What am I doing wrong???

Hello! I have set up 3 2D animations - Idle; Walk; Jump;

To have the Idle → Walk transition happen the float parameter “Speed” needs to be greater than 0.1. I have that being set in my player movement script. Everything seems to work fine except the fact that the transition happens only when moving left even though the code is the same for both sides. Please take a look and help me out. :wink:

P.S. I already submitted the same post on the Animation forum but nobody was able to help me there.

using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
     public float speed = 1f;
     public float jumpHeight = 100f;
     public bool onGround = true;
     public Transform jumpCheckEnd;
     Animator animator;
     void Start() {
         animator = GetComponent<Animator>();
     }
     void Update() {
         if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow)) {
             transform.Translate (Vector2.right * speed * Time.deltaTime);
             transform.GetComponent<SpriteRenderer> ().flipX = false;
             animator.SetFloat ("Speed", 1f);
         } else {
             animator.SetFloat ("Speed", 0f);
         }
         if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow)) {
             transform.Translate (Vector2.left * speed * Time.deltaTime);
             transform.GetComponent<SpriteRenderer> ().flipX = true;
             animator.SetFloat("Speed", 1f);
         } else {
             animator.SetFloat("Speed", 0f);
         }
         Debug.DrawLine(this.transform.position, jumpCheckEnd.position, Color.white);
         onGround = Physics2D.Linecast(this.transform.position, jumpCheckEnd.position, 1 << LayerMask.NameToLayer("Ground"));
         if (Input.GetKeyDown(KeyCode.Space) && onGround == true) {
             GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpHeight);
         }
         if (onGround == true) {
             animator.SetBool("onGround", true);  
         } else {
             animator.SetBool("onGround", false);
         }
     }
}

Hello,

If the “Speed” is set to 1 in “Right code”, you reset it to 0 in the “Left code”. You can do something like :

         if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow)) {
             transform.Translate (Vector2.right * speed * Time.deltaTime);
             transform.GetComponent<SpriteRenderer> ().flipX = false;
            animator.SetFloat ("Speed", 1f);
         }
         else if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow)) {
             transform.Translate (Vector2.left * speed * Time.deltaTime);
             transform.GetComponent<SpriteRenderer> ().flipX = true;
             animator.SetFloat("Speed", 1f);
         } else {
             animator.SetFloat("Speed", 0f);
         }

Thank you so much! This actually worked. You’re the first person to even try to help me. You’re awesome. :wink: