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?
Same thing here, did you found any solution ? thanks !
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.
the trick is working, thanks !!!
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);
This doesn't seem to work if I play on enable.
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! =)
This work perfectly for mine! Thanks
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.
Great news ! Need to try that ;)
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 badben
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" );
GetComponent<Animator> ().ResetTrigger ( "Play" );
And setup the Animator like this:
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
14 People are following this question.
Mecanim loop pose
Mecanim how to make a the shooting animation fire rapidly
"Add" animation to current position?
Mecanim position specific bone
Newly Created Animations not Playing with Mecanim