- Home /
How Do I Check if another key was pushed while one key was being held down?
I have animations and movement in my game rigged up to the same key presses. I need to have the animation of walking to the right change to walking to the left on a given key press. However, when I try this, if the right key is being held down, it doesn't register that the left key was pushed and is now being held down. So I can essentially be holding left, or right, and my character will walk in the opposite direction. The code is the unholy abomination below.`using UnityEngine; using System.Collections;
public class Player : MonoBehaviour { public Animator PlayerAnimator; public float moveHorizontal; public float speed; public SpriteRenderer PlayerRenderer; public Sprite[] PlayerSprites;
void Start ()
{
}
void Update ()
{
PlayerMovement();
}
void PlayerMovement()
{
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
{
moveHorizontal = -1f;
PlayerAnimator.SetTrigger("PlayerWalkLeft");
}
else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
{
moveHorizontal = 1f;
PlayerAnimator.SetTrigger("PlayerWalkRight");
}
/*****************/
/******************************************************************************/
if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.RightArrow))
{
moveHorizontal = 0f;
PlayerAnimator.SetTrigger("PlayerLeftFacingIdle");
}
else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.LeftArrow))
{
moveHorizontal = 0f;
PlayerAnimator.SetTrigger("PlayerRightFacingIdle");
}
/*************************************/
/****************************************************/
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
if(Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
{
moveHorizontal = 1f;
PlayerAnimator.SetTrigger("PlayerWalkRight");
}
}
else if(Input.GetKey(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
{
if(Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
{
moveHorizontal = -1f;
PlayerAnimator.SetTrigger("PlayerWalkLeft");
}
}
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveHorizontal * speed, 0);
}
}
`
I'd change this a bit:
On one of the key downs, set that direction as the current (by moveHorizontal and playAnimation). On key up, check if the opposite key is still held and set that.
Right now you're kind of overriding things because you're checking held buttons with an if else which gives A precedence over D because of the execution order.
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
moveHorizontal = -1f;
PlayerAnimator.SetTrigger("PlayerWalkLeft");
}
else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
{
moveHorizontal = 1f;
PlayerAnimator.SetTrigger("PlayerWalkRight");
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A))
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{
moveHorizontal = 1f;
PlayerAnimator.SetTrigger("PlayerWalkRight");
}
{
moveHorizontal = 0f;
PlayerAnimator.SetTrigger("PlayerRightFacingIdle");
}
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.D))
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
moveHorizontal = -1f;
PlayerAnimator.SetTrigger("PlayerWalkLeft");
}
{
moveHorizontal = 0f;
PlayerAnimator.SetTrigger("PlayerRightFacingIdle");
}
}
There's probably an even better way, but right now I don't have the time to think this through