Building a Weapon Cooldown System in Unity

Matthew Givens
2 min readApr 22, 2021

--

One component of nearly every game with weapons is a cooldown system. As fun as it is to shoot things in game, without a cooldown most games would be unbalanced. There are different ways to go about building this feature, buy here we will go over the method using Unity’s built in time tracker. Our pseudo code could look something like the following.

Essentially we simply want Unity to keep track of how much time has passed since our last shot, and what the rate of fire should be. To do this we will require two variables in addition to the existing instantiate statement. Both variables should be floats because we are working with time. First the variable “_fireRate” will do the obvious and allow us to choose the delay in seconds. Last “_canFire” will play an important role in calculating the amount of time that has passed since our last shot.

In our code we will use the logical AND operator so that it only runs if both conditions are true. The first line is using Time.time to check if the time that has passed in game is longer than the amount of time in which we can fire again. Though for this to function, we must add our fire rate to Time.time and assign than number to the “_canFire” variable. Now the second condition in our if statement logic won’t be true unless at the amount of time that has passed is at least the assigned fire rate.

--

--

Matthew Givens

A self-taught Unity developer, learning more everyday and documenting my journey. My passion for the world of game development grows with every skill I pick up.