Simple Cooldown System in Unity

Siddhant Thakur
3 min readMar 24, 2021

You got the laser firing(click here to know how we got the laser firing), but at this point, you can spam the shots which are not ideal. To counter this you can create a system where after each shot you wait for a few seconds before you’re allowed to fire another shot.

To implement a Cooldown System we need the following,

1) Figure out how much time has passed since the last shot
2) Check if the time passed since the last shot is within your acceptable range

Now, lets get to these in detail.

Figuring out how much time has passed since the last shot

This can be done with the help of,

Time.time, gives us time in seconds since the start of the game. This will help us keep track of when the shots were fired. Let’s say if we were to fire a shot 3 minutes after the game started, the Time.time would print 300 seconds.

The first shot needs to be immediate that is, it requires no cooldown. We can create a variable which will let us know when we can fire a shot.

With this we can do the following,

This will allow us to shoot but with no cooldown

Check if the time since the last shot is within our acceptable range

Let’s start off by creating a variable which defines our minimum time before we’re allowed to fire the next shot.

It’s Serialized so that we can choose an ideal value at run time from the editor

With this cooldownTime vairable we can alter the canFire variable to allow us to shoot every three seconds.

Putting it all together we get the following,

FireLaser is a method containig our Logic for Firing

This gives us the following result

Here we can observe that our Laser has achieved a cooldown of three seconds.

--

--

Siddhant Thakur

Aspiring Game Developer with experience in the field of Machine Learning.