有没有一种方法可以从基于加速的AnimationCurve中计算线性AnimationCurve?

时间:2019-05-14 01:35:44

标签: c# unity3d unity-editor

我必须使用AnimationCurves生成一个AnimationClip,以将对象移动到目标位置。为此,我编写了一个编辑脚本,该脚本可生成AnimationClips并配置所有内容。

现在我的问题是: 我有一个速度曲线,该速度曲线定义了对象将在何时,如何快速移动。 (1是TopSpeed,0没有移动)看起来像这样。 https://i.imgur.com/44YDHrY.png

它产生什么 https://i.imgur.com/kzsT8RL.png

现在,我尝试了几种生成曲线的方法。 其中linearCurve是用于在起始位置和目标位置之间进行插值的曲线 speedCurve定义了如上所述的速度曲线

worldCurve是预期的输出。

AnimationCurve linearCurve = AnimationCurve.Linear(0f, startZ, clipLength, destinationZ);
AnimationCurve speedCurve = distanceData.speedCurve;

AnimationCurve worldCurve = new AnimationCurve();
int sampleRate = 3;

float _scTime = 0f;
float _scValue = 0f;

for (int i = 0; i < speedCurve.length; i++) {
    //float scTime = (clipLength / sampleRate) * i;
    float scTime = speedCurve.keys[i].time;
    float scValue = speedCurve.keys[i].value;
    Debug.Log(String.Format("{0} hat {1}", scTime, scValue));
}

float lastZ = 0f;
for (int i = 0; i < speedCurve.length; i++) {
    //float scTime = (clipLength / sampleRate) * i;
    float scTime = speedCurve.keys[i].time;
    float scValue = speedCurve.keys[i].value;

    //Debug.Log(String.Format("{0} von {1}", scNow, scMax));

    //float sampleTime = /*linearCurve.length * */(_scTime + (Math.Abs(scTime - _scTime)));
    float calcZ = lastZ;
    float sampleTime = 0f;
    if (i > 0) {
        for (int j = 0; j < sampleRate; j++) {
            float tI = (float)((float)j / (float)sampleRate);
            float tTime = (scTime - _scTime) * tI;

            // speedCurve.keys.Last().time
            float sampleValue = linearCurve.Evaluate(scTime);

            sampleTime = clipLength * (i / speedCurve.length) + tTime;

            //float diffZ = linearCurve.Evaluate(scTime) - linearCurve.Evaluate(_scTime);

            //calcZ = lastZ + diffZ;
            Keyframe kf = new Keyframe(sampleTime, sampleValue);
            worldCurve.AddKey(kf);
        }
    }



    _scTime = sampleTime;// scTime;
    _scValue = scValue;
    lastZ = calcZ;
}

ac.SetCurve("World/Fake", typeof(Transform), "localPosition.z", worldCurve);

它应生成如下曲线:https://i.imgur.com/pY8dm5g.png

0 个答案:

没有答案