Unity: Creating A Cooldown System

Amber Chunn
3 min readJun 28, 2022

--

Use Cooldown Systems to Excite and Engage Your Players

Pedestrian walk sign with “Wait” lit up
Photo by Valou _c on Unsplash

When I was learning to drive, my mom showed me how to “trip” a stoplight. In California, many of our stoplights where triggered by the weight of the car. If you could put your car over the underground sensor, it would “trip” the light so that it would turn in your favor and you didn’t have to wait until the light timer switched the lights. This is the metaphor that comes to my mind when thinking of cooldown systems.

Sometimes in games, you want the player to control when an action or event begins. Other times, you want the player to wait for a minimum duration before they can carry out the next action — also known as a “cooldown period”. One of the most obvious places we can see this in action is when the player attacks an enemy.

First, before we jump into Unity and code, let’s give some thought as to why we want to put this cooldown system into place. In our game, we have a special attack that becomes available to the player once they’ve gathered enough magical goo. The player’s special ability becomes active once their magical goo meter is full.

This obstacle (goo meter) stands between the player being able to barrage an enemy with their special ability, and the degree of challenge of the game. It creates tension when players are waiting for their meter to fill up. Then, with the special ability active, the player triggers the ability and demolishes the enemy. The excitement a player feels in that moment of victory is directly impacted by the cooldown system. This might seem a bit counterintuitive, but the player’s experience is heightened by the fact that they had to wait until they collected enough of the magical goo before being able to trigger the giant attack.

Now that we have a solid foundation of what a cooldown system is and why we would want to use one in our game, let’s learn how we can implement a simple cooldown system in Unity. For our player’s character, they need to accumulate magical goo in order to activate their special ability.

  1. First, we need to create a variable for the player’s current goo & rate of goo accumulation
    private int _gooMeter
    private int _gooRate
  2. We also need a variable to tell us when our special ability has been triggered. Be sure to set the variable to false, as it isn’t active by default.
    private bool isSpecialAbilityActive = false
  3. Next, we will need to make sure that our Update() method is tracking our _gooMeter so that it can change the isSpecialAttackActive variable to true whenever the _gooMeter is full – which in our case is 20 units. For this we will also make use of Time.deltaTime().
  4. Now that we’re tracking our goo and checking for our variable, we can add the conditional to the Update() function as well. For this, we check if the variable is true, and if the player pressed the special attack trigger. In our case we’ve set the trigger to the “K” button. This is because we don’t want to force the player to use the special attack until they want to.
  5. For the final piece, we will add our cooldown system. Unity uses a special item called “Coroutines”. Coroutines allow us to set a ‘yield’ for your function and spread actions over a series of frames. In fact, your Coroutine must contain a yield statement to be correct. Note that coroutines also start a bit differently than usual functions, like so: IEnumerator followed by the name of the coroutine.
  6. Once our player uses their special attack, we will kick off our SpecialAttackCooldownRoutine(). This allows the special attack to continue for some duration of time, and then sets the isSpecialAttackActive variable back to false.
private int _gooMeter;
private int _gooRate = 2;
private bool isSpecialAbilityActive = false;
void Update()
{
_gooMeter += Time.deltaTime * _gooRate;

if (_gooMeter >= 20){
_isSpecialAbilityActive = true;
}
if (isSpecialAbilityActive == true && Input.GetKeyDown(KeyCode."K")){
SpecialAttack();
}
}
void SpecialAttack() {
_isSpecialAbilityActive = true;
Instantiate(_specialAttackPrefab, transform.position, Quaternion.identity);

StartCoroutine(SpecialAttackCooldownRoutine());
}
IEnumerator SpecialAttackCooldownRoutine() {
yield return new WaitForSeconds(2.0f);
_isSpecialAbilityActive = false;
}

I hope you’ve enjoyed our exploration of cooldown systems. As you continue in your game development journey, you will find tons of uses for coroutines and different types of cooldown mechanisms.

Happy Gaming!

--

--