Some Cooldown Logic in Unity (2D Space Shooter)

Marcus Ansley
2 min readMar 25, 2021

Implemented some cooldown logic to the 2D Space Shooter project recently, admittedly based to a large extent off the Unity Documetation on Input.GetButton (as I’m trying to use this more than Input.GetKey): https://docs.unity3d.com/ScriptReference/Input.GetButton.html

The logic works by checking to see whether Time.time (the time passed since the start of the game) is greater than the _nextFire float. The _nextFire float has a default value, for example 0, but with each successful fire it’s value is reassigned to Time.time + the _fireRate float (the delay or ‘cooldown’ we want enforce).

Cooldowns can have a lot of applications, but in the case of our space shooter we’re implementing it as a means of preventing the player from being too overpowered (and affecting the game’s overall balance and, ultimately, its challenge and enjoyability).

I do find it quite interesting how adding a limitation or restriction gives the player’s actions more meaning. Like if we give the player the most powerful attack in the game but make them wait an agonising 60s before they can use it again (various superweapons and special powers in a variety of games, particualrly MOBAS and strategy games, tend to make use of these). It reminds me a bit of a favourite Eliot quote of mine from awhile back:

‘freedom is only truly freedom when it appears against the background of an artificial limitation’

The idea that, by restricting something, we give it value. Anyway, I’m certainly digressing with this one (it is, after all, just a delay between the shots of a laser):

--

--

Marcus Ansley

Game Developer | Game Design and Literature Graduate