Search Unity

Set a character to a specific point in animation in the editor

Discussion in 'Animation' started by Tom163, Jan 10, 2021.

  1. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Similar to https://forum.unity.com/threads/stopping-an-animation-on-a-specific-frame.692260/ but I want this to work in the editor.

    I have animated characters that I want to set into a specific pose (for a freeze-frame picture). I'd like to select an animation for them, then move through that animation, and then pick that point and hold it, and apply it to the character. Essentially, a script that allows me to say "put this character into frame 25 in animation "jump" " or similar.

    My characters are UMA characters, so the actual body is generated at runtime, which is why I can't just put them into a chosen pose in an external 3D tool. It also uses the Animator component, not the legacy animation (for which I have found solutions while searching).

    I can find the frame and animation I need with the existing tools, but I can't find a "set the animation to this frame" function.
     
  2. Xriuk

    Xriuk

    Joined:
    Aug 10, 2018
    Posts:
    21
    You can use Animator.Play but you need to calculate the frame you want to be at.
    Then you can update manually the animator to apply it to your character, you may want to set the speed to 0, to prevent the animation from moving
     
  3. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Yes, I understand setting speed to 0 to freez the animation is needed. But how do I manually update the animator? Aside from running it in play mode until it's there and then updating the speed to 0 in the inspector. I'd like a scripted approach where I find the frame I want in the inspector and then enter it somewhere - so that I can press play and it'll jump to that frame (or animate until it reaches that frame, that's fine as well).
     
  4. Xriuk

    Xriuk

    Joined:
    Aug 10, 2018
    Posts:
    21
    This plays your animation at a specified frame (which you need to calculate in normalizedTime):
    https://docs.unity3d.com/ScriptReference/Animator.Play.html

    And this updates the animator manually, you can pass it a deltaTime of 1/60, it really doesn't matter since you have set the speed to 0:
    https://docs.unity3d.com/ScriptReference/Animator.Update.html

    After calling them in order inside your Editor script you'll have your character frozen at that particular frame.
    Note that this operation cannot be reverted as if you were in Play Mode since you are actually overwriting animated gameobjects' positions and rotations. So if you need to restore something later you'll have to save the current status and restore later manually.
     
    Tom163 likes this.
  5. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    That works like a charm. Thanks !