How to create a cooldown system in Unity

A cooldown is a time in which a weapon’s capacity or use is unavailable or decreased. In video games, weapons are added cooldown systems to balance out the gameplay with the amount of power given to the player vs the obstacles they face. Some modern games add it as a sense of realism to simulate real-life firearms.

Essentially, we are controlling the fire rate of our player’s weapon. The fire rate refers to how often a weapon can fire or launch its projectiles. It is normally measured in rounds per minute (RPM or round/min) or rounds per second (RPS or round/s) in modern firearms.

The unit and logic we will be using consist of measuring time per second. In Unity, this can be done via Time.time

Use Time.time to see how many seconds have elapsed since the game started runtime.

Next, we build a variable that will be compared to how long the game has been running. This variable will also determine when the player can shoot again.

We will be able to tell whether 0.15 seconds have passed by using this variable. Then, the value of this variable will get re-assigned by calculating the current runtime plus the cooldown/delay value. This action happens every time the weapon is fired, however, we still need a variable that allows for this breath to take place.

The SerializeField will be used to tweak the value from the inspector if necessary.

Now, check if Time.Time is greater than the current _nextShot . If that’s the case, we can fire and then set the value of _nextShot to the current time + the _fireDelay.

Before

After

In my next article, we’ll be taking a look at Rubber Duck Debugging!

An ambitious creative writer, and video game narrative designer on a journey to become a Unity Game Developer, and Sofware Engineer.

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