Weapon cooldown system in Unity

Daniel Kirwan
2 min readMar 29, 2021

--

In a previous article I talked about instantiating game objects in Unity and showed a project I’m working on where I was constantly firing lasers.

Allowing the player to continuously fire the lasers in this game is not what I intend the gameplay to be. How can we fix this? We implement a cooldown system and set a fire rate for the player. The plan is to eventually implement a weapon class with child classes that have their own separate fire rates, but for now, we can implement a simple cooldown system using Time.time.

Time.time is how long the game has been running in seconds. We implement this by following along with the documentation found here: https://docs.unity3d.com/ScriptReference/Time-time.html

We need a fire rate variable which is the time wanted between shots and a nextFire variable which is the time until the player can next fire the laser.

You can see how simple it is to create a simple cool-down system in Unity. We have added a check to the pressing of the space key and every time we fire the _nextFire variable is updated to be Time.time + the fireRate.

Below you can see the [Header(“”)] attribute available in Unity. It gives better organisation of the variables in the Unity editor and gives designers an easier time with knowing what they’re changing.

You can see below the effects of the cool-down system. Once it is working we can adjust the fireRate in the editor to get a good fire rate for the player.

That's all we have time for folks, come back for the next article.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet