Question by
DBether · Dec 25, 2015 at 11:44 PM ·
2dscripting problem
Weird bug with animations in 2D.
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. ;)
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);
}
}
}
Comment
Your answer