- Home /
Jump to a specific frame in an animator
How can I jump and stop on a specific frame in an animator?
Answer by nullgarden · May 03, 2017 at 07:51 AM
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 https://docs.unity3d.com/ScriptReference/Animator.Play.html
So far I could not figure out a way to specify a location based off a particular frame index.
@d2clon this is so it freezes on that specific frame and shows as a static image. If you want the animation to continue from that point then you don't need to touch speed.
Answer by FM-Productions · May 03, 2017 at 07:51 AM
Hi, I think this is what you are looking for: https://docs.unity3d.com/ScriptReference/Animator.PlayInFixedTime.html https://docs.unity3d.com/ScriptReference/Experimental.Director.IAnimatorControllerPlayable.PlayInFixedTime.html
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.
Answer by petzichila · Aug 25, 2020 at 04:02 PM
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!
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Get Animation from Animator 2 Answers
Animator conditions, no bool state selector. 1 Answer
Run sprite animation in editor mode? 0 Answers
Trying to access mecanim floats C#, 1 Answer