- Home /
Flash image while ammo is low - coroutines?
Basically what I want to do is have a "no ammo" image in the UI flashing (fading in and out repeatedly) whenever the player's ammo is gone. I think I understand how to use Lerp but what I'm not sure about is how to make the process continue indefinitely until the player picks up more ammo. Would I constantly run a check in the Update() function, and if so, what would I do after that to make the flashing be continuous instead of constantly restarting the flashing every frame? I feel like this might involve coroutines, which I'm not particularly experienced with. Thanks for your help!
Answer by Agent000 · Mar 25, 2015 at 07:31 AM
Thanks for the help. About an hour after posting this question I realized I had completely forgotten about Unity's built in animator. It ended up being super easy to do using that.
Answer by Firedan1176 · Mar 23, 2015 at 10:07 PM
You could use coroutines, but I like to just use Update.
float counter;
float countdown;
void Start() {
countdown = counter;
}
void Update() {
if(countdown <= 0) {
//Do flashy stuff here
countdown = counter;
}
countdown -= Time.deltaTime;
}
Answer by Prosto_Lyubo · Mar 23, 2015 at 10:46 PM
There is easier solution. Add Image to Your UI, then on Image Component call (only once ie on event, when You want to show the image - NOT in Update):
Image Mysplashscreen;
float targetalpha = 0f;
float duration = 1.5f; //in seconds
Mysplashscreen.CrossFadeAlpha(targetalpha, duration, true);
Your answer
Follow this Question
Related Questions
Can you run a coroutine more often than Update? 1 Answer
Coroutine is jerky after exiting trigger zone 1 Answer
How to make functions async? 2 Answers
move inside a coroutine 2 Answers
How to slow down large code? 1 Answer