Saad Khawaja Menu

Viewing all items for tag animation

Permalink:

Play a specific frame of Animation (Legacy)

Unity does not have a method to goto a specific frame per say. But you can move to a specific time interval of an animation using Gameobject.animation["name"].time – Legacy Animation

So in short, you can actually goto a specific frame by finding the time of the animation at that particular frame. e.g. If the animation speed (visible in the animation window) is 24 fps: It means 24 frames are rendered in 1 sec.

The first frame will be at 1/24 * 1. The second will be at 1/24 * 2. and so on.

So this snippet will do the trick:

gameobjectname.animation.Play ("Take 001");
gameobjectname.animation.Stop ();
gameobjectname.animation["Take 001"].speed = 0; //to make the animation pause
gameobjectname.animation["Take 001"].enabled = true;
gameobjectname.animation["Take 001"].time = (1f/24f)*frameNumber;
  • Smithc669

    I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again! aeddcaddgbgdaeef

  • martin

    i was looking at this for my own use and im sure it will work however im not sure how to initialise frameNumber and the code is deprecated from unity 5 trying to swap this for GetComponent works fine for .Play but not the rest it seems any ideas?

  • Thanks for leaving a comment, please keep it clean. HTML allowed is strong, code and a href.