Cooldown System

Creating a system timer

Having something spawn infinity at the speed of light is all fun and games until you realized that logic doesn’t work that way. Therefore we have a timer, a cooldown system to prevent abuse of certain features. In this case, it shall be to limit the firing rate of our shooting cube.

Rapid fire looks fun and dandy, until it makes your game too easy

How to apply a Cooldown System

First, we have to understand how a cooldown system works. “Time.time” is the scripting API that counts the seconds that has past in real time. So, if you playing for 5 seconds and then pause that would mean Time.time = 5. Every second the game is running would equal the Time.time. The only issue is, Time.time is every ongoing if I wanted to fire every 2 seconds after the first shot I would need Time.time to compare the time to something else. Therefore we will need to create another variable.

Cooldown script (fig.1)

The fig. 1 example, we have created two float variables on top. “Fire rate” and “can fire”. Fire rate is how long I want to wait until the next bullet gets fire, and “can fire” is the time it that is being compared to real time (aka Time.time).

Cooldown script (fig.2)

In the fig.2 example is the logic behind the cooldown system. How it reads is:

When the keycode “space” is pushed, check the real time to see if we can fire. We can only fire when the bullet has left the player after a certain set time. Therefore _canFire = the real time + _fireRate.

If you have fired the bullet and it only has been 0.5 seconds, _canFire = 0.6 seconds but real time has only been 0.5 seconds, therefore we can not fire again until real time has past 0.6 seconds marks. This is how the cooldown system works.

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.