- Home /
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;
}
}
Answer by SarperS · Oct 22, 2014 at 02:23 PM
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;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613165646im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
lerp in coroutine doesn't work correctly 0 Answers
restrict Lerp to only one axis? 1 Answer
Lerping multiple materials of a gameobject within a coroutine 0 Answers
Lerping blinks 1 Answer