how to restart mecanim state animation or play it from random position [SOLVED]

we’ve been looking for the answer almost week and we found it and it’s pretty simple, so we decided to share for those who are looking too :smiley: there’s no need for tricks with empty or duplicate state …

I'm not sure why this question is closed. There are a variety of other solutions for fixing this problem and it currently sits at the top of the Google search. For example it can be done purely through an animator transition. Or with an Animator Behavior script for re-usability and better code abstraction. I'm about 50 points away from being able to re-open this question. Can an admin with more points than me please re-open this?

A post over 6 years old and still massively helpful. Has just helped me with a proble I was struggling with for a long time.

Hi komondor, I think using animator.Play(0) or animator.Play(null) can restart the current state too...

I've re-opened this question as there are a variety of potential solutions not covered and it's still sitting at the top of google searches.

Beautiful. Thank you so much. The documentation @Box_Frog shared, https://docs.unity3d.com/ScriptReference/Animator.Play.html, is also very clutch if you are interested in how or why this works.

3 Answers

3

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);

Nice, thanks you guy, This solved my problem.

Thank you so much! I didn't take a week, but I did take like two hours trying to figure this out. Might have been a week if you didn't already put the week in for me...

You are a genius!

What does the -1 do though? Doesn't it work with the correct layer's index?

great help, thank you!

Beautiful. Thank you so much. The documentation @Box_Frog shared, Unity - Scripting API: Animator.Play, is also very clutch if you are interested in how or why this works.

Beautiful. Thank you so much. The documentation @Box_Frog shared, Unity - Scripting API: Animator.Play, is also very clutch if you are interested in how or why this works.