在 Unity3D 中获取给定时间的动画剪辑的位置



我想检索给定时间/帧的动画剪辑的变换/位置。

我实际上可以使用animationClip.SampleAnimation(((https://docs.unity3d.com/ScriptReference/AnimationClip.SampleAnimation.html(将animationClip设置为给定时间

因此,我最终可以创建一个克隆,将其放在特定的帧上并得到他的位置,但对于每个帧来说都太重了。

如果您有任何解决方案,或者一些我没有看到的功能,我会很高兴。

谢谢

> 我已经找到了解决方案,这需要很多过程,但由于它仅在编辑器中完成,因此不会影响游戏。 我正在获取动画剪辑的曲线,并存储 10 个位置,每个位置对应于动画的 10%。

为此,我需要计算每个 x、y 和 z 轴曲线:

void OnValidate()
{
//Check if we are in the Editor, and the animationClip is getting change
if (Application.isEditor && !Application.isPlaying && previousClip != animationClip)
{
previousClip = animationClip;
m_checkPoints = new Vector3[10];
int index = 0;
//Taking the curves form the animation clip
AnimationCurve curve;
var curveBindings = AnimationUtility.GetCurveBindings(animationClip);
//Going into each curves
foreach (var curveBinding in curveBindings)
{
//Checking curve's name, as we only need those about the position
if (curveBinding.propertyName == "m_LocalPosition.x")
index = 0;
else if (curveBinding.propertyName == "m_LocalPosition.y")
index = 1;
else if (curveBinding.propertyName == "m_LocalPosition.z")
index = 2;
else
continue;
//Get the curveform the editor
curve = AnimationUtility.GetEditorCurve(animationClip, curveBinding);
for (float i = 0; i < 10; i++)
{
//Evaluate the curves at a given time
m_checkPoints[(int)i][index] = curve.Evaluate(animationClip.length * (i / 10f));
}
}
}
}

最新更新