Slow your roll — Creating a simple Cooldown System

Kenneth Skodje
2 min readMar 22, 2021

--

Before and after cooldown implementation

Having the player being able to fire non-stop is not always the right decision for a game. Here I’ll go over one way you could go about creating a cooldown system.

Time.time allows us to get the time in seconds since the game started. This is an easy way to compare against how long since we did something.

For our player, we will create a variable private float _laserRechargeTime = 0.8f to set the minimum time between shots to 0.8 seconds.
We also need to create a variable (_canFire) to compare Time.time with, to determine if enough time has passed, and we’re allowed to take our next shot.

Whenever we take a shot, we then set _canFire to be the current time + the delay. And whenever we hit the space bar to fire a laser, it also checks to see if the current time is greater than the canFire value. If it is, a shot is fired and a new value set for the next allowed time to fire.

if (Input.GetKeyDown(KeyCode.Space) && _canFire < Time.time)
{
Instantiate(_laserPrefab, transform.position, Quaternion.identity);
_canFire = Time.time + _laserRechargeTime;
}

That’s it, a simple cooldown system that you can easily adjust by just changing a one variable 😁

--

--

No responses yet