Is it possible to "seek" in animation?

The video simulates what I’m trying to achieve:

2024-08-0316-57-42-ezgif.com-video-to-gif-converter

Goal is to dynamically “play” an animation based on another object’s position through script.

  • Something similar to what a blend tree achieves, but it’s for a machine with parts moving, so wouldn’t work
  • I also couldn’t make it work using LERP, as it would control a more complex animation in the end.
  • While jumping to closest keyframes would work, I’d like to use the Animator’s interpolation (e.g. to smooth out a 15 FPS animation).

Tried to search for this, but I couldn’t find the right keywords most likely.

Thanks!

You could normalize the range of the other object’s position as it plays, then use those values as the animation position argument in animation.Play()
You’d need to know the min and max values of the other object’s position.

Thanks! It might work for me, but is there a way to use Play() without actually playing the animation from that point, in a “paused” state?

Or should I just send Play each frame, not sure how performant that would be?

I looked for hours and hours yesterday, but suddenly I found two solutions somehow (how much keywords matter I guess)

  1. Animator > Animation State > Motion Time as Parameter, and set the parameter each frame. Perfect!
  2. AnimationState.time look like a great alternative though.

Nice!
You might want to use AnimationState.normalizedTime, so the full range (of any clip) is 0-1 and you don’t need to know how long it is. This means normalizing whatever parameters you’re sending to it, but you’d have to do that either way.

Some other threads mentioned setting the AnimationState.speed to 0 as well, this would keep it from trying to play after jumping to your desired frame/timepoint.