- Home /
Two lerps in the same loop, but one takes longer, why?
I'm doing two lerps simultaneously, one for centering the camera, other for zooming, using this code:
IEnumerator centerCameraAndZoom(Vector2 source, Vector2 target, float duration, float zoomAmount) {
float t = 0;
while (t < 1) {
// add the time
t += Time.deltaTime / duration;
// smooth it out (easing)
var lerped = Mathf.SmoothStep(0.0f, 1.0f, t);
// zoom in
Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, zoomAmount, lerped);
// move canvas center in the direction of the card
canvas.GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(source, target, lerped);
yield return null;
}
} My problem is that, even though both are theorecitally lerping for the same speed (t is the same for both), the motion of the canvas center takes much longer, resulting on a delayed moving after zoom is done.
Any ideas on how to deal with this, while still keeping the code inside of a single loop?
Answer by FortisVenaliter · Jul 14, 2017 at 03:53 PM
The reason is because you have different sources. On the Camera orthographic size, your start point is the current size. On the position, your start point is the actual start point. So, the position is lerping. But the size actually isn't. Each frame it will lerp a smaller and smaller amount since the gap will constantly be closing.
What you want to do is cache the orthographicSize when you start the lerp, and use that as the first argument in that line.
Your answer

Follow this Question
Related Questions
Apply multiple Lerps to the game value 1 Answer
[SOLVED] Rotation/angled offset in Quaternion.Lerp for a carried GameObject 2 Answers
Is there any way to make lerp work consistantly in update? C# 1 Answer
Remove delay between lerps 1 Answer
How can I Lerp the Rotation of my Game Object to my Mouse Pointer? 1 Answer