Creating a Simple Cool-Down System in Unity

Erin Kerr
3 min readSep 4, 2022

A quick guide for introducing wait times between activities in your game

Example torpedo game without cool down system

Say you are building game where the player is a ship that drops torpedoes on enemies below. So far, you’ve built your player, player movement and are even instantiating the torpedoes. However, like example above, you can drop torpedoes as fast as you can press the Space bar. For obvious reasons, this does not create the desired effect.

To implement a cool down system and force a wait time between torpedoes, you will need:

  • A variable to store the wait time
  • A variable to store the next time a torpedo can be dropped
  • A process to compare the game run time to the next torpedo time
  • A process to update the next torpedo time each time a torpedo is dropped

Variable for Wait Time

Declaring a variable for wait time

First, create a new float variable to store the number of seconds between torpedoes. As always, you can add the SerializeField attribute above to adjust the wait time value in the Inspector.

Variable for Next Torpedo Time

Declaring a variable for next torpedo time

Second, create another new float variable to store the time when the torpedo can be used again. At the start of the game, you can set this to -1 to ensure a torpedo can be fired as soon as the game begins.

Comparing Run Time to Next Torpedo Time

Example torpedo fire code, before and after cooldown system

Next, you’ll want to add a condition to the torpedo fire code to check if the game run time exceeds the next torpedo time. In Unity, game run time is stored in the Time.time variable. Compare to see if Time.time is greater than next torpedo time — if so, the torpedo can be dropped.

Updating Next Torpedo Time

Updating next torpedo time after each torpedo use

Lastly, each time a torpedo is used, be sure to update the next time a torpedo can be fired. To do this, assign the sum of Time.time and the wait time variable created earlier as the new value of next torpedo time. This will ensure that the wait time is honored between uses of the torpedo.

Example torpedo game after implementing the cool down system

Now, no matter how many times the Space key is pressed in between, the torpedo cannot be fired again until the two second wait time has passed. This is just one of many uses for a cool down system. How will you introduce wait times into your projects?

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response