How can I jump and stop on a specific frame in an animator?
Since a lot of other form answers use an outdated Animation component I figured I’d give a simple answer using the Animator class since I’ve been struggling with this and finally got it to work.
private Animator anim;
void Start () {
anim = GetComponent<Animator>();
anim.speed = 0f;
anim.Play("YOUR_ANIMATION_NAME_HERE",0,YOUR_TIME_INDEX_HERE);
}
Where TIME_INDEX_HERE is a number between 0f-1f that represents the time location of your desired frame. (i.e. enter 0f for your first frame and 0.5f for your middle frame). For more info see Animator.Play Unity - Scripting API: Animator.Play
So far I could not figure out a way to specify a location based off a particular frame index.
Hi, I think this is what you are looking for:
Another possibility is to create different subanimations for one animation in your import settings of your base animation fil. In the “Animations” tab, simply add more animations and set the desired start and end frames. Then use these new animations that start at the desired frame of your base animation.
Interesting moment that all variation of Play() have default value for normalized time parameter set as float.NegativeInfinity, and if you stay it as is Animator does do nothing if it now in specified state, so you shoud set it to 0 if you wanna to launch state from start. Simply say Animator interpreties float.NegativeInfinity not as 0 in context of normalized time, what is confusing.
I had this issue. Apparently, both Play() and PlayInFixedTime() multiply the offset by the animation speed. So if your speed is 1, it will behave as expected, but a speed of 2 will mean will animation starts at double the offset. Accordingly, speed 0 means the offset is completely ignored. I don’t know who decided to implement this horrible functionality and then leave it undocumented, but my best guess is an agent of evil, perhaps even Satan himself.
My solution is to set the speed to a value close to 0, such as 10 ^ -10. And if I want an offset of 15 seconds, I set the offset to 15 ^ 10.
This way, these methods will work the same as they would with a speed of 0 if they had been implemented sensibly. For good measure, you should set the speed to exactly 0 in the next frame.
Hope this helps!