协程中的Lerp未运行

时间:2019-05-10 19:26:58

标签: unity3d

我正在使用下面的协程尝试在一秒钟内(从scalingTime)缩放从Vector3.zero到Vector3.one的转换。我确定协程肯定在运行,但是对象没有缩放。我在while循环中正确使用“ yield return null”吗?

IEnumerator ScaleLaser()
{
    float elapsedTime = 0;
    float scalingTime = 1;
    Vector3 currentScale = laser.localScale;

    while (elapsedTime < scalingTime)
    {
        transform.localScale = Vector3.Lerp(currentScale, Vector3.one, elapsedTime / scalingTime);
        elapsedTime += Time.deltaTime;
        yield return null;
    }
}

1 个答案:

答案 0 :(得分:2)

这应该有效。

IEnumerator ScaleLaser()
{
    float scalingTime = 1;
    float time = 0;

    while (time < 1)
    {
        time += Time.deltaTime / scalingTime;
        laser.localScale = Vector3.Lerp(laser.localScale, Vector3.one, time);
        yield return null;
    }
}