How to create a cooldown system in Unity

Dennisse Pagán Dávila
3 min readMar 25, 2021

--

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.

How are cooldown systems made?

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.

Results

Before

After

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

--

--

Dennisse Pagán Dávila
Dennisse Pagán Dávila

Written by Dennisse Pagán Dávila

Software Engineer that specialize in Game Development. Currently looking for new opportunities. Portfolio: dennissepagan.com

No responses yet