Lerping inside Coroutine occurring too quickly

Hi,

I’m doing a camera (orthographic) zoom Lerp inside a Coroutine and have set a specific time for it to complete it in. However, it appears to be completing the Lerp (to the correct target size) much more quickly than intended. For example, for CamMoveDuration I have entered 10 seconds, but the Lerp completes in around 2. The while loop does continue for the correct time, though.

Not too sure what the problem is so any suggestions would be helpful. Thanks in in advance.

IEnumerator ZoomCamera()
{
    doZoom = false;

    float elapsedTime = 0;

    while (elapsedTime < CamMoveDuration)
    {
        camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, camSize, (elapsedTime / CamMoveDuration));
        Debug.Log("camera.orthographicSize = " + camera.orthographicSize);

        elapsedTime += Time.deltaTime;
        Debug.Log("elapsedTime = " + elapsedTime);

        yield return null;
    }
}

The problem is the below line

camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, camSize, (elapsedTime / CamMoveDuration));

Every time you invoke the lerp you restart it from the current orthographic size. What you should do instead is start it from the initial value. Please try the below code.

IEnumerator ZoomCamera()
 {
     doZoom = false;
     float elapsedTime = 0;
     float initialOrthoSize = camera.orthographicSize;
 
     while (elapsedTime < CamMoveDuration)
     {
         camera.orthographicSize = Mathf.Lerp(initialOrthoSize, camSize, (elapsedTime / CamMoveDuration));
         Debug.Log("camera.orthographicSize = " + camera.orthographicSize);
 
         elapsedTime += Time.deltaTime;
         Debug.Log("elapsedTime = " + elapsedTime);
 
         yield return null;
     }
 }