Creating a Cooldown System

A step-by-step guide to creating a cooldown system that you can implement into your games or programs.

Raymond Mills
3 min readJul 17, 2023

Note: There is always more than one way to achieve what you are trying to do. There is no right or wrong way, as long as it works. Although there may be a more efficient way to perform your function and more optimal way to keep your code organized.

In this step-by-step tutorial, we’ll learn how to manually create a cooldown system in Unity using C#. With this system, you can control when players can use a specific action again, how long until an enemy or teammate can respawn, or how long until you can reenter your password because you got it wrong too many times.

For this example, I am making a simple 2D shooter with basic primitives. As this is still just a prototype, this will work fine. The cooldown will be for controlling how fast the player can fire lasers.

My last article showed how to make the player fire lasers, if you need help with this check out Instantiating & Destroying GameObjects in Unity. Building on the last lesson, we just need to add a few lines of code to control the rate of fire.

Step 1: Edit Your Player Script

  1. Inside the PlayerBehavior script, let’s define the variables we need.
_canFire will be the ability to fire your laser and _fireRate will be the cooldown time before you can use _canFire again.

2. We’ll add these variables to the previous code to achieve the desired effect.

&& is a two boolean expression, it returns true if both expressions are true; otherwise, it returns false. Time.time is the amount of real time that has elapsed in this frame.

Now when the Space key is pressed and the value of Time.time is greater than the value of _canFire, the program will run the FireLaser method.

3. Now we can add this controlled fire rate to the FireLaser method. This is the expression that instantiates our LaserPrefab.

Now we can only fire lasers if _canfire = Time.time + _fireRate

Step 2: Testing the Cooldown

  1. Click the “Playbutton to enter Play Mode.
  2. You should now have a controlled rate of fire with an adjustable variable inside the inspector window.
I added sprites to all my game objects, but this will work fine with just the primitive shapes we were working with.

Summary

This system allows you to control the timing of actions in your games, adding strategic elements. Feel free to experiment and integrate cooldowns into various mechanics, such as abilities, attacks, or item usage.

By adjusting cooldown durations and combining them with other game systems, you can bring depth and balance to your game design. Don’t forget to optimize your code and consider visual or audio feedback to enhance the player experience. Take your games to the next level with a well-crafted cooldown system. My next few articles are going to cover simple enemy behaviors. But first, Introduction to Physics in Unity.

--

--

Raymond Mills
Raymond Mills

Written by Raymond Mills

Unity Software Engineer, I am in an apprenticeship with GameDevHQ. I am taking the steps to become a professional Unity Developer.

No responses yet