Is it possible to retrieve the position and orientation of an object with an animation? For example, an animation I create in the animation editor. The time would have to be specified, of course.
I guess I’m looking for something like Animation.Sample() except that it just gives me a Transform to use elsewhere rather than actually affecting the object itself. I’d like to be able to get at the information without actually affecting the object which has the animation.
Not an ideal solution, but… can you store the current animation time in a variable, set the position with animation.Sample, get the transform and then use Sample again to go back to the original position? Visually, this shouldn’t make any difference, because the animation isn’t actually applied until after the Update function.
I’m afraid I’m not able to do that, no. Sometimes an animation will be playing on the object, but other times, no animation will be playing at all. Even if an animation is playing on the object, it will typically not be the animation that I need to sample from.
Is there no other way of retrieving this information?
I don’t think there is any straightforward way to do this. There is a function AnimationUtility.GetAllCurves that can extract curves from an AnimationClip but it is in the editor API rather than the runtime API. It might be possible for you to extract data from the animation curves using an editor script, store it in an array on an object and then use it to create new curves at runtime. If you can get access to an AnimationCurve at runtime, you can sample the curve using the Evaluate function. Unfortunately, there isn’t a function in the AnimationClip class to retrieve curves from an existing clip.
An editor script would be absolutely fine. Thanks for that Andeeee. Great suggestion!