Journal 08 — Time for a Cooldown in Unity
In the previous Journal, I mentioned a need to briefly pause when firing lasers, so the user cannot just spam the spacebar endlessly.
Sort of unfair, right?
In the Player C# script, when pressing the spacebar, we have added a cooldown condition.
&& — means AND, so pressing the spacebar and the other condition must be met before being able to fire. In this example, time in seconds must be greater than the user set time delay before the next laser prefab can be instantiated
Time.time — count real time in seconds
_nextFire — a calculation that adds Time.time to a user set time delay between firing
Let’s see how this works:
Seems more fair. If you wanted to, you could add a powerup later that would turn off any delay, or possibly turn on auto-fire by holding the spacebar for a few seconds.
I appreciate you stopping by. I look forward to share more info as I gain experience with Unity and C#.