- Home /
Restart same animation in Mecanim
I'm using Mecanim to handle my animations on my character, which has been great. However, I've hit a problem.
I have an attack animation that's about a second long, and is triggered by a button. In the middle of this animation, if the player presses the same button again, I'd like the attack animation to restart from the beginning. This may be a simple thing, but how might I do that? Mecanim is great at handling transitions from one animation to another, but how would it restart it's own animation?
Answer by digiben · Feb 17, 2013 at 11:35 PM
Not totally sure but it sounds like someone from Unity said: "We wont support API control over transition in 4.0. So only in the UI.. for now", so a quick way if it isn't doable in the code that you can find, you can create a state in the UI and have it transition to the same animation (and maybe back again?) if you set a certain flag.
Actually, that works great. It's an odd way of thinking about it at first, but in practice, it's quite brilliant. thanks!
sure it work, but it's pretty dirty solution, imagine you have many states and you need all of them doubled ... the Play(state, layer, normalizedTime); works same way but it's cleaner
Answer by komodor · Jan 24, 2014 at 12:14 AM
dunno, but now you can do just:
Animator.Play(state, layer, normalizedTime);
if you use
Animator.Play("same state you are", -1, 0f);
it will restart current state, if you put whatever float from 0 to 1 into normalized time, you can set it to any time position you want
you can also use
Animator.CrossFade(state, crossFadeTime, layer, normalizedTime);
I am not sure where the problem is, but it just works for me, i am switching left and right foot with it and there's no problem
This worked perfectly for me. There was no way I was going to start doubling up on states like the selected answer suggested, so I'm very glad you posted this solution. Thank you! =)
It does work on the host, but failed to sync across the Network Animator
Answer by AnomalusUndrdog · Jul 28, 2013 at 03:37 AM
EDIT: This doesn't work in Unity 5, as mentioned in the comments.
Just a note, at least in Unity 4.1.5, you can use Animator.ForceStateNormalizedTime which takes in a normalized float (0.0 to 1.0 range) to force the playback position of the currently active state (supposedly won't work while in transition).
So you can do something like myAnimator.ForceStateNormalizedTime(0.0f)
to replay the current animation.
This stopped about two hours of headache over animation - this should be more... normal... where in the F is Animator.ResetAnimation(); at???
Thumbs up to you good sir! Hope it doesn't get phased out in 5x - I see it is already marked as deprecated!
Please note this is no longer valid. You have to set the play time when calling AnimatorRef.Play("state", layer, playbackTime). See @komodor's answer for more details.
Answer by BenoitFreslon · Dec 06, 2016 at 02:13 PM
Actually the most easiest method is to set a Trigger parameter to relaunch the same animation.
GetComponent<Animator> ().SetTrigger ( "Play" );
Then
GetComponent<Animator> ().ResetTrigger ( "Play" );
And setup the Animator like this:
Answer by zachstronaut · Aug 01, 2019 at 06:08 PM
If you're trying to accomplish something like this from the animation controller state machine instead of from code, you're going to want to uncheck Has Exit Time
and check Can Transition to Self
on the transition from Any State to your state.