Creating a Cooldown System in Unity

Chris Hilton
3 min readJun 9, 2021

Objective: To control our player’s laser shooting through the use of a cooldown system in Unity.

Currently the player can spam the Space key and shoot the laser as much as they want which is going to give our enemies a hard time and ultimately make for a pretty boring game where you never lose!

Solution: Let’s add a cooldown system to our laser!

Spamming the Space key but the player can only fire every 0.25seconds

Let’s Add Some Code

Open up our ‘Player’ script and add the following code which we will have a look at:

Laser Cooldown System

Let’s break this down:

public float canFire = 0f;
// Create a new float variable called canFire and this will help us to determine whether we are able to fire our laser or not

public float fireRate = 0.25f;
// Create a new float variable called fireRate and assign this a value 0f 0.25f. This will be the time required between laser shots. If you want the player to wait longer to shoot their laser or even quicker, this is where you would change it.

if (Input.GetKeyDown(KeyCode.Space) && Time.time > canFire)
// From our previous code we have added (&& Time.time > canFire). We now require 2 conditional statements to read true, before the below code can execute. If the player presses the Space key but Time.time is less than canFire, it won’t shoot. E.g. To start the game, canFire is set to 0, so at any stage from starting the game you should be able to fire your first laser as Time.time will always be greater than 0.

{
canFire = Time.time + fireRate;

// Once, the above 2 conditions have been met and your first laser has been fired, let’s assign Time.time + fireRate to canFire. E.g. If we press the Space key at time 1.0s and we add our fireRate of 0.25, then canFire becomes 1.25 AND until Time.time is now greater than 1.25, the laser won’t fire again, thus creating our cooldown system!

Instantiate(laserPrefab, transform.position + _laserOffset, Quaternion.identity);
// Taken from previous code, but simply instantiates a laser beam when the above 2 conditions have been met, at the players transform.position with an offset so it doesn’t shoot from the middle of the player and it’s rotation remains the same.
}

Refactoring Our Code:
We can now take this code a step further and tidy it up a little bit as it is highly likely that we will be adding more features to our laser and we don’t want all this code hanging around in the Update() method. What happens if your Update() method is hundreds of lines long and you want to try find a piece of code that is causing some issues. You want to be able to call on separate methods that not only makes it look cleaner and easier to read, but also help you pin point an issue if you for example have an issue with your laser. You know to head straight to your FireLaser() method. So let’s go ahead and do the following:

Highlight all this code, then right click on it and choose the top option: “Quick Actions and Refactoring” and then click “Extract Method” and it will automatically create a new private method for your code, all you have to do is name it appropriately, which I have called FireLaser().

Refactoring our Code

So now, within our Update() method, if the player presses the Space key and Time.time is greater than canFire, execute (or call on) the FireLaser() method.

Useful Links:
https://docs.unity3d.com/ScriptReference/Time-time.html

--

--