Quantcast
Viewing latest article 8
Browse Latest Browse All 24

Why does this Lerp take twice as long as I expect?

I am animating a box onto a panel (on a Canvas) using a simple LERP in a Coroutine. I take a duration parameter, normalize it, and then apply the LERP. However, I am noticing that the amount of time for the LERP to do it's thing is 2x the time I expect. - ie: when duration = 1.0f, then it takes 2 seconds to perform the LERP (as indicated in my Debug.Log messages below) - ie: when duration = 0.5f, then it takes 1 seconds to perform the LERP What am I missing? Is my WaitForSeconds impacting the calculation? And if so, why is this the case? protected IEnumerator SlideIntoPanelHelper (float duration) { Debug.Log("Sliding into place at Time: " + Time.time); float i = 0.0f; float step = 1.0f / duration; // normalize the duration to [0..1] RectTransform rt = GetComponent(); Vector3 start = rt.anchoredPosition3D; Vector3 end = rt.anchoredPosition3D; end.x = 0; while (i <= 1.0f) { float ndt = step * Time.deltaTime; i += ndt; rt.anchoredPosition3D = Vector3.Lerp(start, end, i); yield return new WaitForSeconds(0.02f); } Debug.Log("Sliding into place COMPLETE at Time: " + Time.time); }

Viewing latest article 8
Browse Latest Browse All 24

Trending Articles