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 :D 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 ad$$anonymous$$ 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.
Answer by komodor · Jan 24, 2014 at 12:19 AM
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);
Thank you so much! I didn't take a week, but I did take like two hours trying to figure this out. $$anonymous$$ight have been a week if you didn't already put the week in for me...
It took me like two days until I finally stumbled upon this answer. It is a joke that it often takes so long to find what is ultimately a pretty simple solution to a pretty small problem almost every single time.
What does the -1 do though? Doesn't it work with the correct layer's index?
$$anonymous$$y God, finally something that works.
Why is it often sooo complicated to find a solution to something so simple when I do searches for this stuff in Unity!
Also, I still don't quite understand the "layer" part of this function: What does the "layer" do with an animation?