Lerp and Co例程C#Unity

时间:2018-11-13 17:22:46

标签: c# unity3d transform coroutine lerp

我想将一个对象从一个位置移动到另一个位置。然后再次从该位置移到另一个位置。第一个动作执行Lerp,但是第二个动作似乎只是捕捉而不是束缚其位置。

非常感谢, 马特

using System.Collections;
using UnityEngine;

public class lerp : MonoBehaviour
{
public float TravelTime;
public float TravelTime2;
Vector3 StartingPos;
float Timer;
float duration;
void Start()


{
    StartingPos = transform.position;
    Timer = -1.0f;
    duration = TravelTime + TravelTime2;
}

void Update()

{
    StartCoroutine(move());
}


public IEnumerator move()

{
    while (Timer < TravelTime)
    {
        Timer += Time.deltaTime;
        yield return transform.position = Vector3.Lerp(StartingPos, new Vector3(5, 5, 5), Timer / TravelTime);
      //  yield return new WaitForSeconds(7);
    }

    while (Timer > TravelTime)
    {
        Timer += Time.deltaTime;
        yield return transform.position = Vector3.Lerp(new Vector3(5, 5, 5), new Vector3(5, 15, 5), Timer / TravelTime2);
        yield return null;
     }
}
}

0 个答案:

没有答案