AnimationCurve.Evaluate-通过价值获取时间

Dav*_*rák 4 curve unity-game-engine

有没有一种内置的方法如何从Unity3d中的“动画”曲线按值获取时间?(相反的评估方式)

我需要实现这一目标(而不是从时间上获得价值):

float time = AnimationCurve.Evaluate(float value);
Run Code Online (Sandbox Code Playgroud)

一般来说,从Y值获取X值。

小智 6

我知道这是3岁的孩子,但我是通过Google搜索找到的,以防其他人落入此处:

我只创建一条反曲线,即可按时间查找。

public AnimationCurve speedCurve;

private AnimationCurve inverseSpeedCurve;

private void Start()
{
    //create inverse speedcurve
    inverseSpeedCurve = new AnimationCurve();
    for (int i = 0; i < speedCurve.length; i++)
    {
        Keyframe inverseKey = new Keyframe(speedCurve.keys[i].value, speedCurve.keys[i].time);
        inverseSpeedCurve.AddKey(inverseKey);
    }
}
Run Code Online (Sandbox Code Playgroud)


nex*_*exx 4

只是一个基本的实现,也许它会给你一个想法。方法会循环遍历所有时间,如果您的值接近当时的值,它将产生结果。它是一个协程,但您可以将其更改为在 Update 内使用吗?

public AnimationCurve curve;
public float valToTime = .5f;
public float treshold = .005f;
public float yourTime;
IEnumerator valueToTime(float determineTime)
{
    float timeCounter = 0;
    Keyframe[] k = curve.keys;
    float endTime = k[k.Length-1].time;
    Debug.Log("end "+endTime);
    while(timeCounter < endTime)
    {
        float val = curve.Evaluate(timeCounter);
        Debug.Log("val "+ val + "  time "+timeCounter);
        // have to find a better solution for treshold sometimes it misses(use Update?)!
        if(Mathf.Abs(val - determineTime) < treshold)
        {
            //Your time would be this
            yourTime = timeCounter;
            yield break;
        }
        else 
        {
            //If it's -1 than a problem occured, try changing treshold
            yourTime = -1f;
        }
        timeCounter += Time.deltaTime;
        yield return null;
    }
}
Run Code Online (Sandbox Code Playgroud)