0

所以我有一个脚本来移动游戏对象。当我移动一个游戏对象时,它很流畅。但是当我第二次移动它时,它的移动速度非常慢,看起来有点马车。

移动脚本中的第一个输入是对象,然后是它需要移动到的位置和速度作为最后一个参数。所有坐标都基于本地位置。我使用等待是因为我想在执行第二个动作之前等待。

我也尝试过两次移动其他物体,但它们最终都移动得很慢/有问题。

我不想在更新中运行它,这就是我使用协程的原因。

这是我的代码:

IEnumerator MovementGentryOne()
{
    StartCoroutine(Movement(GentryOne, MovementCoords.GentryOneBasin, gentryspeed));
    yield return new WaitForSeconds(2);
    StartCoroutine(Movement(GentryOneArm, MovementCoords.GentryArmMoved, gentryspeed));
    yield return new WaitForSeconds(2);
    StartCoroutine(Movement(GentryOnePicker, MovementCoords.GentryPickerPick, gentryspeed));

    yield return new WaitForSeconds(4);
//this one is not working smooth.
    StartCoroutine(Movement(GentryOnePicker, MovementCoords.GentryPickerStart, gentryspeed)); 
    yield return null;
}

private IEnumerator Movement(GameObject toMove, Vector3 position, float time)
{

    float elapsedTime = 0;
    while (elapsedTime < time)
    {
        toMove.transform.localPosition = Vector3.Lerp(toMove.transform.localPosition, position, (elapsedTime / time));
        elapsedTime += Time.deltaTime;
        yield return null;
    }
    toMove.transform.localPosition = position;
}

有人知道出了什么问题吗?

亲切的问候

4

1 回答 1

0

我找到了答案,该方法永远不会脱离 while 循环。所以我将 while 语句编辑为: toMove.transform.localPosition != position

于 2017-02-15T12:56:15.153 回答