- Home /
Capture positions and rotations of all bones at a specific frame of an animation clip
In Start(), I'd like to somehow play the first frame of a particular animation for my character model and then capture all the positions and rotations of the bone's transforms in arrays, without the animation actually visibly playing (I just need that information for later, but I don't really want to visibly play this particular animation at Start()).
Any ideas on how I might accomplish this?
Answer by Paulius-Liekis · May 08, 2011 at 09:47 AM
You should do this: sample the frame on the animation you want, then restore character pose to the previous one.
You can sample you desired animation by doing something like this:
AnimationState state = animation["name"]; state.weight = 1; state.enabled = true; state.normalizedTime = 0; animation.Sample();
// extract the desired transforms
// get all child transforms Transform[] transforms = animation.gameObject.GetComponentsInChildren<Transform>();
// Collect position of all transforms (declare this dict somewere where you can access it later) Dictionary<Transform, Vector3> positions = new Dictionary<Transfrom, Vector3>(); foreach (Transform t in transforms) positions.Add(t, t.localPosition);
// restore previous pose (by either sampling another animation, // or saving and restoring transform values) animation.Stop(); animation.Play("Run");
Restoring positions later:
foreach(KeyValuePair<Transfrom, Vector3> entry in positions)
{
entry.key.localPosition = entry.value;
}
Obviously you want to store positions and/or rotations and maybe scale. Depends on what you do.
I thought about using Sample(), but I have no idea what it actually does and the docs seem deliberately scarce. What do I do with it once I've sampled it? Where do those transform values and whatnot get saved to?
Transform values are saved on the object itself (i.e. on object transforms), which means that if you do not restore values of transforms then the character will be rendered in that pose.
The previous pose is not an animation though, so I'm not sure how I will utilize Sample()... in short, I have an animation called "getup" that is the character standing up from a laying position. When the player is knocked down I simply enable all the kinematic rigidbodies on his bones, then from whatever position he settles in I want to crawl his bone hierarchy and disable the kinematics, then lerp the positions and rotations to the first frame of the getup animation. Does that make any sense?
Yes, that makes sense. I do such things every time need to blend between animation and ragdoll. You just have to store "get up" pose and then use it for lerping.
I still don't understand how to use the Sample() data though. One I Sample() the get up animation at the first frame, where is that bone transform data stored so I can lerp the ragdoll bone transforms to those positions? Can you expand your example code a little bit to show how to use what you Sample()?
Your answer
![](https://koobas.hobune.stream/wayback/20220613132517im_/https://answers.unity.com/themes/thub/images/avi.jpg)