I'm able to Lerp the color of a UI panel within the Update method. However I'd like to trigger behavior a single time when needed. If I put it in a method and call it it only changes for a brief time and doesn't fully complete. I could probably set a bool flag within the update method but that seems sloppy.
1 Answer
\$\begingroup\$
\$\endgroup\$
1
Using a Coroutine gives the desired effect.
IEnumerator ChangeColorTest()
{
for (var i = lerpDuration; i >= 0; i -= 0.1f)
{
UIPanel.color = Color.Lerp(startColor, endColor, Time.time / lerpDuration);
yield return new WaitForSeconds(0.1f);
}
}
I'd like the color transition to be smoother but this might be how Coroutines work.
-
5\$\begingroup\$ "this might be how Coroutines work" nope. You're waiting a tenth of a second (ie. 3-6 frames) between iterations instead of updating the colour every frame (which you can do with
yield return null
). You're also using the wrong calculation of the blend parameter to adjust for time. \$\endgroup\$– DMGregory ♦Commented Mar 11, 2019 at 19:23