Orthographic Camera size lerping

Hey guys, I’ve been having an issue with coroutines and lerping together. Basically, everytime resizeCamera() is called, i want the camera size to increase/decrease dependant on the gobal variable named size. However, when resizeCamera() is called the game freezes as if the coroutine isn’t being run as a coroutine(even though i’m using startCoroutine). Any help would be appreciated,

 private void resizeCamera()
        {
            StartCoroutine(resizeRoutine(camMain.orthographicSize, (size / 50) * 70, 0.5f));
        }
    
        private IEnumerator resizeRoutine(float oldSize, float newSize, float time)
        {
            while (oldSize != newSize)
            {
                camMain.orthographicSize = Mathf.Lerp(oldSize, newSize, time);
                yield return null;
            }
        }

Thanks!
Bazzalisk

You’re misusing Lerp :smiley:

The third parameter is not time, but a value between 0 and 1. When t=0, Lerp returns oldSize, when t=1, it returns newSize. Any value in between gets interpolated.

To make it work, do this:

private IEnumerator resizeRoutine(float oldSize, float newSize, float time)
{
    float elapsed = 0;
    while (elapsed <= time)
    {
        elapsed += Time.deltaTime;
        float t = Mathf.Clamp01(elapsed / time);

        camMain.orthographicSize = Mathf.Lerp(oldSize, newSize, t);
        yield return null;
    }
}

Thank you, lifesaver!