- Home /
Player plays walking animation if I press a and d at the same time while standing still
I'm new to programming and I'm trying to write a simple idle to walk animations script. But if I press a and d (left and right keys) at the same time the player stops in place and plays the walking animation.
As I'm a noob, I'm writing my code using lots of bools. I use them so I can reference the player's actions in another script, the one that controls animations.
I have tried everything I could think of, but I can't seem to solve it.
Here's the walking-idle part of my code:
// isWalking
if (Input.GetKeyDown("a") && isGrounded)
{
isWalking = true;
}
if (Input.GetKeyDown(KeyCode.D) && isGrounded)
{
isWalking = true;
}
if (!Input.anyKey)
{
isWalking = false;
}
if (isGrounded == false)
{
isWalking = false;
}
And here's the walking-idle animation part of my other script (pM refers to the other script):
// Idle
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D) && pM.isWalking == false)
{
anim.SetBool("isWalking", false);
}
// isWalking
if (pM.isWalking == true)
{
anim.SetBool("isWalking", true);
}
if (pM.isWalking == false)
{
anim.SetBool("isWalking", false);
}
This is my first post. Any help would be appreciated!
Answer by mzaidan1996 · Jul 24, 2020 at 04:24 PM
So after you keep tapping A and D, you get stuck playing the walking animation even though your standing still?
Answer by Marhola · Jul 24, 2020 at 05:22 PM
Yeah, exactly.
I think it is because of the high number of booleans your using, maybe it got something to do with it, review all your script that has the booleans, try to find the issue, maybe 2 booleans are contradicting each other? Not sure.
Have you tried seeing if there was a problem in the animator window? You can see which animations are playing from there and when youre not pressing a or d, you can set the animation state to an empty default, or an idle phase. Also, you should be using triggers instead of bools.
Thank you all for your advice! I'll look at it.