Creating A Cooldown System in Unity

Dustin Cargile
2 min readOct 27, 2022

--

So, you’ve got a Player and you’ve got a projectile that the player can shoot at enemies, but you’re Player can shoot entirely too many projectiles way too quickly. This may make a game that is extremely fun for some people, but it is probably going to get boring really quickly if there is zero challenge.

We can limit the amount of time between shots to at least give the enemies a fighting chance.

In the Player script, we will need 2 float variables. One for fireRate and another to determine if enough time has passed so that we canFire.

canFire is -1 to ensure we can fire immediately upon loading

Note the SerializeField attribute above the fireRate variable. This is so any designers can adjust the fireRate in the Inspector. It is not present above the canFire variable as we only want to be able to change that with code.

Next, we will implement our variables. We need a way to check if enough time has passed so that when we hit the Spacebar the Player will fire. We do this by using Time.time, which keeps track of how much time has passed since the application loaded.

Comparing Time.time to our canFire and then loading Time.time plus fireRate into canFire when we successfully fire will help us achieve our desired result.

And now when we save and run in Unity. We should only be able to fire every half second.

Now if you change the fireRate, you can shoot faster or slower determined by the value you select.

And that’s it! A working cooldown system. There a several other things that this can be applied to, such as powerups and building timers and other things.

--

--