web analytics

Play Animation From Frame : Unity 5 Animator

Lets say you want to play the animation from the mid frame .This is how u will do it

getComponent<Animator>().Play(“Animation_Name”,0,0.5f);

Unity3d play animation from frame

Lets say you want to play your animation from a specific frame

getComponent<Animator>().Play(“Animation_Name”, 0 , (1/total_frames)*frame_number);

You can find the total_frames by selecting the Fbx that contains those animations .

eg. _animator.Play ("BalloonUp",0,(1f/109f)*20f);   //This will start my “BalloonUp” animation from frame number 20

unity play animation from frame 3
Unity play animation from frame to frame :

_animator.Play ("BalloonUp", 0, (1f / total_frames) * from_frame);
Invoke ("STOPAnimation", (anim_length / total_frames) * to_frame);

void STOPAnimation ()
{
_animator.speed = 0f;
}

 

Before playing your animation make sure to set your Animation Speed to 1f
unity play animation from frame

How to get animation length unity : Animator

_animator = GetComponent<Animator> ();
    RuntimeAnimatorController ac = _animator.runtimeAnimatorController;    //Get Animator controller
    for (int i = 0; i<ac.animationClips.Length; i++) {
      if (ac.animationClips [i].name == "UP") {        //If it has the same name as your clip
          anim_length = ac.animationClips [i].length;
      }
    }

unity play animation from frame 2

unity play animation from frame

Example :

// This code plays the animation from frame 20 to 60

public class AnimatorTest : MonoBehaviour
{

  private Animator _animator;
  private float anim_length;

  // Use this for initialization
  void Start ()
  {
    _animator = GetComponent<Animator> ();
    RuntimeAnimatorController ac = _animator.runtimeAnimatorController;    //Get Animator controller
    for (int i = 0; i<ac.animationClips.Length; i++) {
      if (ac.animationClips [i].name == "UP") {        //If it has the same name as your clip
        anim_length = ac.animationClips [i].length;
      }
    }

    _animator.Play ("BalloonUp", 0, (1f / 109f) * 20f); // 109 = total frames , 20 = start frame
    Invoke ("StopAnim", (_up_anim_length / 109f) * 60f);
  }

  void StopAnim() 
 {
    _animator.speed = 0f;
 }

This the Animator we are using –

unity play animation from frame

 

*Lets say you want to resume from the last stopped frame , you can use

 

float jumpTime= _animator.GetCurrentAnimatorStateInfo(0).normalizedTime;

_animator.Play ("BalloonUp", 0,jumpTime);

GetCurrentAnimatorStateInfo(int layerIndex) – Gives you the state of currently playing animation .
Normalized Time – A value of 1 is the end of the animation. A value of 0.5 is the middle of the animation.

For Legacy Animation Use Below : 

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:

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

You may also like...

5 Responses

  1. Railon says:

    Hi,
    great tutorial. How can I reset an animation?
    E.g. I’ve got a animation playing and the user can trigger to exit the animation at any time. I then want the animation to reset, so it is at frame 0.0f again.

    Thanks

  2. Admin says:

    Hi , You can simply Re-activate your gameobject to reset you animation Or you can use “_animator .Play(“same state you are”, 0, 0f);”

Leave a Reply

Your email address will not be published. Required fields are marked *