- Home /
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.
What is the transition back condition in your animator? Check and make sure you let the animation transition back only after the Exit Time of the animation
Answer by FerretallicA · Sep 24, 2014 at 08:52 PM
1) make sure you turn looping off for the kicking animation
2) 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.
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.
Answer by neondrop · Apr 13, 2014 at 03:59 PM
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 -.-
Your answer
![](https://koobas.hobune.stream/wayback/20220613142618im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Triggering an animation once in mecanim 4 Answers
[Mecanim] How To Play Animation? 1 Answer
Add more bones to skeleton - vertebraes definition...? 0 Answers
How to create a 2D rig? 1 Answer
Multiple Animation Events not firing 1 Answer