Running an animation completely before transitioning back (Mecanim)

Basically I want to run an animation once when I press a key. And no matter how long the key is pressed I want to run it once completely and then transition automatically back to idle state.

I really have tried to find a solution regarding to this, because there are many similiar questions on the web, but I couldn’t find anything that explains this in detail, because I’m completely new to Unity.

		if (Input.GetKeyDown (KeyCode.E)) {
			anim.SetBool ("Kicking", true);
		}

		if(anim.IsInTransition(0) && anim.GetNextAnimatorStateInfo(0).nameHash == kickStateHash){
			anim.SetBool("Kicking", false);
		}

This is basically the code, the problem is that when I press ‘E’ it doesn’t run ‘Kick’ all the way through but cuts it in the middle. I know why it’s doing this, but don’t know how to fix it.

I also tried this:

    if (Input.GetKeyDown (KeyCode.E)) {
		anim.SetBool ("Kicking", true);
	}

	if (anim.GetCurrentAnimatorStateInfo(0).nameHash == kickStateHash) {
	} else {
		anim.SetBool ("Kicking", false);}

But it doesn’t work at all. (I don’t get any errors but the animation isn’t -visibly- playing)

I also tried with a Trigger instead of a Bool, but then again the animation only plays halfway and then transitions back to Idle.

  1. make sure you turn looping off for the kicking animation

32900-unity2.png

  1. for the transition from Kick animation to Idle (or whatever you want it to go back to), make sure you set both ExitTime to 1 and Kicking to false.

32899-unity1.png

This will ensure that the animation will run once, and only once, then return to the Idle animation only when Kicking is false and the animation has completed in full.

Sorry guys, it seems I’m really stupid. I have the solution now. In case anyone is looking for this as well: Instead of making it complicated and trying to set a bool to false to transition back into idle, simply use exit time in the animator -.-