Creating A Cool Down System in Unity
--
Objective:
Create a 2-second cool-down system.
We need to know if two seconds has passed. In order to do this, we can use:
Time.time allows us to detect how many seconds has passed. The value of Time.time is how long the game has been running. ie. 300 = 5 minutes.
Now that we can detect how long the game has been running, we need to create a variable to check it against. This variable will act as the trigger for when we can fire next.
This variable is going to allow us to determine if 2 seconds has passed. When we fire the object, we’ll re-assign this variable to the current time, plus a cool down delay.
That means we need a cooldown delay variable.
Now that we have our data, we can check to see if Time.Time is greater than the current canFire value. If it is, then we can fire and then reassign the value of canFire to the current time + the fireDelay.
Now we’re left with a working cooldown system
Because we have a variable that’s controlling the fireDelay, we can serialize that field and allow a designer to come in and change its value.