Coroutines give us the power to easily implement logic for waiting a certain amount of time. This is perfect for a cool down system. Let’s take a look.
First we need a control variable to handle when we can and can’t fire.
We also need a variable to determine how long our cooldown period will be.
Now, in Update() when we are checking for laser fire input, we can also check to see “_canFire” is true.
If we’re able to fire, we immediately set that bool to “false” so it will only run once until our bool is set to “true” again.
Now, for the main cooldown logic. We’ll use a coroutine to wait “cooldownTime” seconds and pass that in as a parameter to the coroutine.
There we go. A simple, yet flexible cooldown system in just a few mins.