Search Unity

Step through and pause each frame in current animator state in code

Discussion in 'Animation' started by MonoFlux, Jul 7, 2022.

  1. MonoFlux

    MonoFlux

    Joined:
    Jun 13, 2015
    Posts:
    39
    Good day.

    I want to be able to programatically step through and pause on each frame of a current animation on a 3D model - in game.

    For example: the 3D model, playing a Walk Cycle, should be paused on frame 1 of the current state and when I press a key, the model should play the next frame (the pose changes by 1 frame). Additionally, I would need to know when the animation has reached its final frame.

    I would live to avoid having to create animation events for each frame in the animation.- which is the only solution I can think of :confused:

    I have found various threads on how to determine current frame and how to set the animation time etc... but I am battling to get information on this exact process.

    Thanks!
     
  2. AAAAAAAAAE

    AAAAAAAAAE

    Joined:
    Jun 8, 2013
    Posts:
    100
    //0 is first clip of animator layer
    //layer_index layer you are accessing

    AnimationClip clip = animator.GetCurrentAnimatorClipInfo(layer_index)[clip_index].clip;
    AnimatorStateInfo current_state = animator.GetCurrentAnimatorStateInfo(layer_index);

    float max_frames = clip.length * clip.frameRate;

    float current frame = current_state.normalizedTime * max_frames;
     
    Last edited: Nov 24, 2023