Simple Cool-down System in Unity3D

Alberto Garcia
3 min readDec 8, 2022

The idea comes from systems that over-heat and need some time to “cool down” in order for them to start working again. A sweaty, fatigued runner taking a quick break, a steam machine getting too hot to work correctly, a car’s engine activating the radiator. There are many examples of systems that may use a cool-down technique. More commonly in games, this term is related to a timer, a count down of waiting time for the player to start using certain resources or systems once again.

Some of the most known examples are reloading a weapon, respawn of collectibles or enemies, and even establishing a time frame for the shooting speed of a weapon. For this example, a time segment will be implemented into a shooting mechanism to give a feeling of rhythm and immersion while shooting a space ship.

Topics:

  • Coroutine
  • Boolean

Imagine a sniper rifle that shoots as fast as an AK-47… it breaks immersion to feel a different sensation than the expected functionality.

Shooting system without cool-down mechanic

The following code combines the use of a type of Coroutine and a type of Boolean to create a time window between each shot, adding difficulty and personality to the manipulation of this weapon.

IEnumerator is an “Interface Enumerator”. In this example, it is used as a coroutine called LaserReloading(), where the yield WaitForSeconds() is used. This function adds a timer function before jumping to the next line of code, which in this case turns the _laserCanFire boolean to true.

A Boolean is a true/false statement, which are very useful to dictate whether certain state is active or passive, on or off, true or false. For this example, the boolean conditions the weapon’s ability to fire, and after shooting, the bool _laserCanFire turns false and the Coroutine LaserReloading() activates, which after 0.35 seconds becomes true again.

This helps to give the weapon a short, time break and not constantly shoot every time the player simply hits the Space bar, but shoot when the Space bar is hit and the boolean _laserCanFire is true.

private bool _laserCanFire = true;

void ShootLaser()
{
if (Input.GetKeyDown(KeyCode.Space) && _laserCanFire)
{
Instantiate(_laserPrefab, transform.position + new Vector3(0,0.8f,0), Quaternion.identity);
_laserCanFire = false;
StartCoroutine(LaserReloading());
}
IEnumerator LaserReloading()
{
yield return new WaitForSeconds(.35f);
_laserCanFire = true;
}
}

The result:

Waiting time of 0.35 seconds between each bullet

Cool-down systems play a huge role in giving games rhythm and cycles that allows players to take actions based on the time-frames given by experience designers. Some people love shooters like Serious Sam because of the infinite shooting constantly occurring, while other gamers prefer a tactical stealth game like Metal Gear where head-shots with a dart gun that shoots once every two seconds rewards players better than using 20 bullets of a machine gun in two seconds, which brings enemy troops running to the noise. These rhythms define the gameplay as well as the need for players to be patient and hit the input buttons wisely timed.

--

--