Card Battlers to FPSs, there isn’t a genre on the market that doesn’t have a game with some sort of cooldown system tucked away in one of it’s mechanics. For some games it’s an entire feature but for others it’s used for simple bookkeeping such as determining when enemies spawn the possibilities of using a cooldown system are endless.
In today’s tutorial we’ll be covering how to make a simple cooldown system using Unity’s UI system and a few bits of code in the context of a Shoot ’em Up game. Start by opening up a fresh Unity project or an existing one and open a fresh scene.
Let’s start off by creating a simple 3D Cube this will act as our player ship that will fire a few projectiles which will form the basis of our cooldown system.
Next let’s add a Unity UI slider to the hierarchy (Right click > UI > Slider) this will represent our cooldown bar and should automatically create a Unity Event System as well as Canvas object in the hierarchy. Rename the slider to “Cooldown” for brevity’s sake. Zero out the rect transform to 0, 0, 0 so it’s centered in the screen.
Next open up the Slider object and let’s take a look at all the parts that make it up. You have the Background, Fill, and Handle. The slider component itself controls the logic of the slider. By default this has a value of 0 to 1 but you can change it to whatever you like. I’m leaving mine default.
For ease of distinction, change the background image tint to another color and the fill image tint to another. I chose red and green for this example, this will help us visualize the slider moving later.
Next we’ll create a simple player script called “CooldownExample” this will be on the Cube and will fire a simple sphere projectile upwards after 1 second passes when the player clicks the mouse buttons. The code for this script is down below I highly encourage to type it out rather than copy/paste.
Next we’ll get to the cooldown system itself. At the top of CooldownExample add a “using UnityEngine.UI;” this will then allow you to add a private Slider variable with a [SerializeField] attribute.
Also add a private bool called “inCooldown” this will gate the cooldown system to player input.
Next drop in the Slider on the CooldownExample script like so. This links the slider to the cooldown system on the player.
Next we’ll go to the Start() method and set the slider.value to 1. This automatically defaults the slider’s value to 100% when the game starts.
Next we’ll go down to the fire method and deduct 0.1f from the cooldownSlider.value with each shot like so.
Let’s test it out to see what we’ve got going for us.
Looking pretty good. We’ve got the deduction half of a cooldown system. Now let’s work on getting it to stop player shooting until it refills up.
So the first thing we need to do is gate the player shooting using the “inCooldown” bool. You can do that by using two ampersands in the if statment in Fire() to add a “and” condition. In this case, if “inCooldown” is false then the player is free to fire otherwise they can’t.
Next we need to add an if statement to the fire condition that if we dip below or equal 0 for the cooldownSlider.value then we need to turn “inCooldown” to true like so.
Next we need add an “else if” statement to the fire condition that handles the cooldown period and regenerating the cooldown slide. We should check to see if we’re in the “inCooldown” state is set to true and also that cooldownSlider.value isn’t 1.
If both conditions are met then we should regenerate the slider until the slider value is at or above 1 then turn “inCooldown” to false letting the player fire again.
Here’s the complete code for posterity, but this should allow out slider to regenerate effectively completing our cooldown loop.
Let’s see our cooldown system in action. I’ve amped up the number deducted each shot from 0.1f to 0.25f to speed things up considerably.
There you have it, a very crude albeit very effective cooldown system. While the system cools down the player is unable to fire leaving them vulnerable to attack. This system is incredibly basic but with it you can extend it to begin regenerating after a second or two without firing, making the cooldown system “lock down” if you overheat, or any sort of interesting mechanic that relies on the player overheating to enable other behaviors.
Hopefully this can be the basis for your very own custom cooldown system and until next time, happy coding.