Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How can I save the state of an animation, and restore it later, during gameplay?

Discussion in 'Animation' started by Dreamback, Jul 21, 2017.

  1. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    220
    I've got a game world with a couple dozen NPCs walking around. During gameplay I need to record their exact animations for playback, with full smooth rewind and fast forward. What can I save from them to do this? Right now I'm saving their position/rotation, and that works, but their animations don't match up.
     
  2. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    220
    Any ideas? Seems to me even without record/playback the ability to hard-set an animation to a specific key-frame with offset would be useful in any game, so there must be a way.

    Edit:
    I found an “Animator.StartRecording”, “Animator.StopRecording”, and “AnimatorPlayback”, but no way to access the recording data to save to disk, nor is there a way to continue recording after playback. I also noticed an oddity, the description mentions “PlaybackRecordedFrame”, but that doesn’t seem to actually exist in the documentation.

    There’s also “Animator.GetCurrentAnimatorStateInfo”, but no call to Set the info, and it doesn’t return the name/path of the animation but the hash of it, so can only be used comparatively. I guess for each and every animated object I’m recording I could get a list of all the animation names and run one by one comparing the hash values, though that seems rather inefficient and slow given that there could be over a hundred of the NPCs on the screen, each with different animation sets, and I’d be doing this every frame.
     
    Last edited: Jul 24, 2017
  3. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    220
    For those who want to do something similar, I have it...almost working. I'm doing GetCurrentAnimatorStateInfo to get the animation name's hash and normalizedTime, and then when playing it back I'm calling Animator.Play, which I realized does have a version accepting a hashed animation name. And it does play the animation, but since I'm doing that every frame, it can be quite jerky at times, I think maybe because the framerate when capturing isn't perfectly synced up with the framerate when playing back, though I'm trying to sync playback up to stored timestamps.