In my game I have it so that when the mouse is pointing on either side of the player and I move the player toward the mouse, it plays a “running forward” animation. On the other hand, when I move the player in the opposite direction of the mouse, it plays a “running backward” animation. So far everything works AS LONG AS I don’t try to input two keys at once.
Here’s my problem: say I’m pointing my mouse on the right of the player and moving right by holding down the “d” key, it plays “running forward”. No problem here. However, if I quickly switch to the “a” key to move left, it still plays “running forward”. This obviously has to do with the inputs interfering because everything works nicely if I properly release a key before pressing another. Here’s a gif to show you what I mean (I recommend playing in fullscreen). My code:
void Update()
{
//horizontal movement speed
horMove = Input.GetAxisRaw("Horizontal") * moveSpeed;
//position of mouse relative to player
var delta = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
//play running backward
if ((delta.x > 0 && rb.velocity.x < 0) || (delta.x < 0 && rb.velocity.x > 0))
{
animator.SetFloat("SpeedBack", Mathf.Abs(horMove));
}
//play running forward
else if (Mathf.Abs(delta.x) > 0 && Mathf.Abs(rb.velocity.x) > 0)
{
animator.SetFloat("Speed", Mathf.Abs(horMove));
}
else //don't play anything
{
animator.SetFloat("Speed", 0f);
animator.SetFloat("SpeedBack", 0f);
}
}
The conditions in my animator are set up such that “running forward” switches to “running backward” when Speed < 0.01 and SpeedBack > 0.01. And when switching from “running backward” to “running forward”, Speed > 0.01 and SpeedBack < 0.01. Any kind of help would be greatly appreciated!