Creating a Cool Down System in Unity

In game development it is common to restrict abilities, events, and many other things from performing actions until a set amount of time has passed. Instructions to perform these delays are called cooldown systems.

In C# we can use the Time modules time() method. Let’s say you created a method to shoot a bullet, like this:

This WILL work, but NOT the way we want it to, as you can see:

What we need to do here is use time(). This method returns the current amount of time the game has been running and we can store that value in a variable. To put it to use we will first create two variable called _fireRate and _canFire.

These should be declared at the beginning, before Start().

NOTE: The reason we set the initial value of _canShoot to -1 is because the game run time starts 0 and we want to be able to shoot as soon as the game starts.

The first variable (_FireRate) will be used to store the amount of time, in seconds, we want to wait before the player is allow to shoot the next bullet.

The second variable (_canFire) will be used to store the current time + the fire rate.

We can then check to see if the current time is a higher value than _canFire with an if statement. If _canFire is less than the current time, the player can shoot another bullet.

Now our ShootBullet() method should look like this:

Resulting in a game that looks like this:

That’s all it takes to create a cooldown with C#!

See you next time!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sage Clark

Sage Clark

I’m a unity developer/ software engineer and I love all things related to game development. Check out my articles on Unity and C#.