1

I'm attempting to grow and then shrink a series of gameobjects using two different lerps within a coroutine. Despite the two while loops being functionally identical, the objects will grow but not shrink afterwards, instead remaining at their increased size.

I've substituted hard-coded values for the initial and final sizes, but the problem still persists. I've tested and found that both loops are executing fine, and the program does not get stuck in either loop.

Is there some fundamental misunderstanding I have of the nature of lerps or coroutines?

    initialSize = transform.localScale.x;
    finalSize = 2f * initialSize;    

    float elapsedTime = 0f;
    while (elapsedTime < trap.pulsateTime)
    {
        transform.localScale = Vector3.Lerp(transform.localScale, finalSize * Vector3.one, (elapsedTime / trap.pulsateTime));
        yield return new WaitForEndOfFrame();
        elapsedTime += Time.deltaTime;
    }
    elapsedTime = 0f;
    while (elapsedTime < trap.pulsateTime)
    {
         transform.localScale = Vector3.Lerp(transform.localScale, initialSize * Vector3.one, (elapsedTime / trap.pulsateTime));
         yield return new WaitForEndOfFrame();
         elapsedTime += Time.deltaTime;
    }
4

1 回答 1

3

为了正确 lerp 你应该存储你的起始值而不是 lerp 当前

float elapsedTime = 0f;
while (elapsedTime < trap.pulsateTime)
{
    transform.localScale = Vector3.Lerp(initialSize, finalSize * Vector3.one, (elapsedTime / trap.pulsateTime));
    yield return new WaitForEndOfFrame();
    elapsedTime += Time.deltaTime;
}
elapsedTime = 0f;
while (elapsedTime < trap.pulsateTime)
{
     transform.localScale = Vector3.Lerp(finalSize , initialSize * 
     Vector3.one, (elapsedTime / trap.pulsateTime));
     yield return new WaitForEndOfFrame();
     elapsedTime += Time.deltaTime;
}
于 2019-12-03T10:02:14.837 回答