GameDev : Cool Down System!

Matteo Lo Piccolo
4 min readMay 15, 2021

--

Now we have to set laser to make us shoot every “n” time or “n” second, because now we can shoot without stop, and in this type of game is not exactly the best, right?

To understand how, we have to understand the concept of TIME inside Unity.

We start from scratch : we create a variable to define the delay from one laser to another.

Again, we use [SerializeField] attribute so in the inspector we can change every time we want without problems.

Where we put it? Inside Player script.

Always make things clear, and we create a fireRate variable

That’s why : the logic here is: IF we hit the space bar AND THE TIME IS PASSED, I can shoot again.
So every time we press the space bar we check if TIME has passed.
How can we control TIME in Unity?

If we search for this on Google

and this is the link we need
https://docs.unity3d.com/ScriptReference/Time-time.html

If we read the documentation here, Time.time represent the TIME PASSED FROM THE START OF THE GAME, so if we make our game run for 10 second, Time.time = 10 second.

Always in the documentation, there is an example to do a cooldown system

As we can see, there is a “fireRate” variable, which represents the delay to shoot our laser, and a “nextFire” variable, which TELLS US IF IT’S OK PRESS SPACE BAR AGAIN.
I know, this can be create a little confusion, but now we go deep to understand the logic behind this statement.

We starting with add this code to our game.
First, nextFire variable

Only private, we don’t want someone change this in teh inspector right?
If someone change it, everything go really bad!

Now, look at this

and implement in our code

Before we add another piece, break this logic to better understand :

The game start, and Time.time is 1.0 seconds, ok?
1.0 is greater than nextFire? Yes, so we can fire the laser.

Here is the TRICKY PART.
We have to ASSIGN nextFire to TIME.TIME PLUS FIRERATE TO CHECK AGAIN IF WE CA FIRE

Break again the logic :

Now nextFire is equal to 1.0f plus fireRate, so 1.5f.

The game continue to run, maybe 1.3f second.
Is 1.3 second greater than nextfire?
NO, because nextFire is 1.5f and we CAN’T FIRE.

The game go forward, Time.time is now 1.6f seconds.
Is 1.6 seconds is greater than nextfire?
YES, because nextFire is 1.5 and we CAN FIRE.

At this point, we CAN FIRE, so the code inside is execute and nextFire is now 1.6 PLUS fireRate, so now is 1.6f + 0.5f.

And so on for the rest of the game!

Hope this explanation helps you better understand how this type of system works.
If not, don’t worry, for 99% of the concepts we just have to use them many times to master them.

Now we try this new system

We can shoot only every 0.5f seconds

And now our fireRate is more controlled!

To optimize, maybe we can change from 0.5f to 0.15f.
This is absolutely personal, you can choose the fireRate you prefer in your game!

We can shoot only every 0.15f seconds

See you in the next article!

--

--

Matteo Lo Piccolo
Matteo Lo Piccolo

Written by Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

No responses yet