1

我有一个大型数据集,其中包含以 0.1 秒为间隔的车辆位置数据,我一直在尝试获取这些数据并沿着这些位置平滑地为游戏对象设置动画。

我尝试过使用 AnimationCurves 并为类似于此的单个轴添加关键帧

    public AnimationCurve ac;
    public AnimationCurve ac1;
    public Vector3 pos1 = new Vector3(0.0f, 0.0f, 0.0f);
    public Vector3 pos2 = new Vector3(0.0f, 0.0f, 0.0f);

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(Move(pos1, pos2, ac, ac1, 3.0f));
        }
    }

    IEnumerator Move(Vector3 pos1, Vector3 pos2, AnimationCurve ac, AnimationCurve ac1, float time)
    {
        float timer = 0.0f;
        while (timer <= time)
        {
            var x = Mathf.Lerp(pos1.x, pos2.x, ac.Evaluate(timer / time));
            var y = Mathf.Lerp(pos1.y, pos2.y, ac1.Evaluate(timer / time));

            transform.position = new Vector3(x, y, 0);
            timer += Time.deltaTime;
            yield return null;
        }
    }

但是我不知道如何将分秒位置数据转换为 AnimationClip 的正确值。

我还尝试使用类似于这样的协程循环位置:

   var coords = vehicles[0].middleCoords;
        var previousStep = coords[0];
        foreach(Vector3 step in coords)
        {
            MoveOverSeconds(step, 0.1f);
        }

  public IEnumerator MoveOverSeconds(Vector3 end, float seconds)
    {
        yield return new WaitForSeconds(seconds);
        float elapsedTime = 0;
        Vector3 startingPos = transform.position;
        while (elapsedTime < seconds)
        {
            transform.position = Vector3.Lerp(startingPos, end, (elapsedTime / seconds));
            elapsedTime += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }
        transform.position = end;

然而,因为 for 循环独立于时间运行,它在另一个结束之前启动协程。任何有关如何解决此问题的帮助将不胜感激。

4

0 回答 0