Animator parameter stuck as True

Hi! I’m using the animator controller for my “attack” animation. It works like a charm if i press the button that’s supposed to trigger the animation in a gentle speed like once every second.
But if i happen to spam press the button let’s say 3 times a second the parameter im using gets stuck as True and the animation gets stuck at the last frame of my attack Animation and wont return to Idle.

if ((Input.GetKey (KeyCode.V)) && !anim.GetBool ("Attack"))
			anim.SetBool ("Attack", true);

Here’s the code i use to trigger the attack animation, i have an Animation event at the last frame of my attack animation to set the Bool back to false so he will return to idle again.

void StopAttack()
	{
		anim.SetBool ("Attack", false);
	}

Okay, I think this will help. Set up your animator like this:

Add the “isAttacking” bool to your parameters in the top left of the animator panel. Make sure there are arrows to and from Idle and Attack. Then select the attack state and look in the inspector. 48998-exampl2.png

Do the same for the Idle state except isAttacking would be false.

Then have your code like this:

if ((Input.GetKeyDown (KeyCode.V)) && !anim.GetBool ("isAttacking"))
            anim.SetBool ("isAttacking", true);

and use however you did to set isAttacking to false again.

I like putting my event handlers for functions similar to the StopAttack() at frame 1 or 2 of an animation. I don’t think there is a reason to call it at the end. Once the transition is triggered there is no reason to keep the condition bool on.

Mine was stuck on true because I had forgotten to add a Condition in the inspector panel for the transition.

animator controller > click on state > uncheck “write defaults”

I did this to all states in my animation controller. It worked for me.