简体   繁体   English

获取Unity3D中给定时间的animationClip的位置

[英]Get the position of an animationClip at a given time in Unity3D

I would like to retrieve the Transform/Position of an animationClip at a given time/frame. 我想在给定的时间/帧处检索animationClip的“变换/位置”。

I can actually set an animationClip to a given time with animationClip.SampleAnimation()( https://docs.unity3d.com/ScriptReference/AnimationClip.SampleAnimation.html ) 我实际上可以使用animationClip.SampleAnimation()( https://docs.unity3d.com/ScriptReference/AnimationClip.SampleAnimation.html )将animationClip设置为给定时间

Therefore I could eventually create a clone, put it on a specific frame and get his position, but it would be too heavy for each frame. 因此,我最终可以创建一个克隆,将其放在特定的框架上并获得他的位置,但是对于每个框架来说,它都太沉重了。

If you have any solutions, or maybe some functions that I didn't see I would be glad. 如果您有任何解决方案,或者某些我没有看到的功能,我会很高兴的。

Thank you 谢谢

I've found the solution, it takes a lot of process, but as it's done only in Editor it doesn't affect the game. 我已经找到了解决方案,它需要很多过程,但是由于仅在编辑器中完成,因此不会影响游戏。 I am getting the curve of the animationclip and I store 10 position, each corresponding to 10% of the animation. 我正在获取动画剪辑的曲线,并存储了10个位置,每个位置对应于动画的10%。

For this I need to compute each x, y and z axis curves : 为此,我需要计算每个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));
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM