- Home /
Trying to Lerp a fill amount according to a countdown coroutine
Hi there.
I have a countdown coroutine for a 5-second shield element in my game:
IEnumerator Flash()
{
countDown = 5f;
invincible = true;
shieldRotator.shieldAppear = true;
while (true)
{
Debug.Log(" count: " + countDown);
yield return new WaitForSeconds(1);
countDown -= 1f;
if (countDown == 0)
{
shieldRotator.shieldAppear = false;
invincible = false;
yield break;
}
}
}
I then have a script on a UI image which reduces the fill of a bar from 1 to 0 depending on this count:
void FixedUpdate()
{
HandleBar();
}
void HandleBar()
{
content.fillAmount = PlayerController.countDown / 5f;
}
How can I Lerp the fill amount so it doesn't jump from 1 to 0.8 to 0.6 to 0.4 to 0.2 to 0?
Answer by Jessespike · Sep 09, 2016 at 04:36 PM
It's skipping because it's instructed to wait for a whole second.
yield return new WaitForSeconds(1);
countDown -= 1f;
Try this instead:
IEnumerator Flash()
{
countDown = 5f;
invincible = true;
shieldRotator.shieldAppear = true;
while (countDown > 0f)
{
Debug.Log(" count: " + countDown);
yield return new WaitForEndOfFrame();
countDown -= Time.deltaTime;;
}
countDown = 0f;
invincible = false;
shieldRotator.shieldAppear = false;
yield return null;
}
Thank you very much.
I had a little play with what you provided and below is the solution. Thanks again.
IEnumerator Flash()
{
countDown = 5f;
invincible = true;
shieldRotator.shieldAppear = true;
while (true)
{
yield return new WaitForEndOfFrame();
countDown -= Time.deltaTime;
if (countDown <= 0)
{
shieldRotator.shieldAppear = false;
invincible = false;
yield break;
}
}
}
Your answer
Follow this Question
Related Questions
object's children not moving with it when using a Lerp-ing coroutine 0 Answers
Smooth damp the lowpass frequency in coroutine: value only changing in 1 frame 1 Answer
[HELP] How to make camera smooth change position while follow object? 1 Answer
If statement not being fullfilled until GameObject inspected in inspector. 0 Answers