I have want my player to start the walk animation when I press the “a” or “d” keys and I want it to stop playing the animation when I release them. The animation plays when I press on of the keys but after I release them the animation plays further to the end before stopping! How can I fix this?
Thx for your help! Here is my code:
using UnityEngine;
using System.Collections;
public class PlayerHorizontal : MonoBehaviour {
public float MoveSpeed;
public bool IsWalking;
public Animator anim;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//set IsWalking
if(Input.GetKey("a")) {
IsWalking = true;
transform.eulerAngles = new Vector2(0 ,180);
transform.Translate(Vector2.right * 4f * Time.deltaTime);
}
if (Input.GetKey("d")) {
IsWalking = true;
transform.eulerAngles = new Vector2(0, 0);
transform.Translate(Vector2.right * 4f * Time.deltaTime);
}
if (Input.GetKeyUp ("a")) {
IsWalking = false;
} else if ((Input.GetKeyUp ("d"))) {
IsWalking = false;
}
anim.SetBool ("IsWalking", IsWalking);
}
}