Creating a Cooldown System in Unity

Germán Walden
2 min readMar 31, 2021

Objective: Create a cooldown system that will restrict our shooting rate in Unity

If you want to check this basic Gameobject instantiation implementation click HERE.

Final result

Requisites: A Gameobject Player that instantiates projectiles by pressing a keyboard key, in this tutorial we are going to use the Space key.

In our player script we are going to need a private firing rate variable, in this case the value will be 0.2f.

Fire rate variable

Remember: Good practices in programming say that we should always keep every variable private unless the value is going to be accessed and modified by an external script.

A private _canFire variable that will tell us if we can fire at the start of the game and after the game time (Time.time) + adding the adjustable firing rate variable time.

Initialized at 0, if check on the Update method if we can shoot

What does Time.time means?

Time.time: It’s the amount of time in seconds the game has being in play mode.

So if Time.time >= _canFire is always true, we will be able to shoot at any moment. How do we fix this? Let’s see this simple approach

With this logic into the if statement we can shoot whenever the time ingame is greater or equal than the time ingame + the adjustable fire rate.

Let’s put together all our previous player movement function and the instantiate projectile.

Final result code

And we are done, remember to adjust the fire rate variable in the Unity Editor to the value you want.

--

--

Germán Walden

Passionate Unity developer always looking for new challenges!