Basic Cooldown System In Unity

Calum Slee
Nerd For Tech
Published in
2 min readApr 27, 2021

Just an additional quick article this afternoon about using Unity’s time function to create a cool down system for our laser.

Firstly, we need to create a variable for our fire rate, personally I found 0.5f or half a second to be ideal thus far.

Next, a variable to check firing our laser against. If we create a canFire variable set it to -1.0f, we know that we will be able to fire straight away in our game, if we wanted a grace period, we could make this a positive number.

To actually check if we can fire. We need to compare it to real-time, Unity’s Time.time allows us to track the runtime of our game in seconds. We can add a condition for our if statement that instantiates our laser prefab. This condition tests whether Time.time is greater than our canFire variable. Currently our runtime is greater than our start value of -1.0f, so if we press the spacebar, a laser will fire.

Now in addition to our Instantiate line of code within our if statement. We can make our canFire variable, equal to Time.time plus our fireRate. For example if current runtime is 20 seconds. Our if statement won’t run true until we are greater than 20.5 seconds run time.

--

--