Just cool down there tiger

Wesley Adams
4 min readApr 5, 2021

--

Cool Tiger

We’re back at it again in Unity 3D and now diving into a new 2D topdown shooter game. Continuing on, there are subtle features we’ll want to add as we progress. One such feature is a firing “cool down” system. When you play most topdown shooters, you wouldn’t want to be able to just spam that fire button. That could make for a super easy game.

Spam the fire button too much

You’ll notice that we have a Laser game object, a “Laser.cs” script for controlling laser bullet commands like destroying the bullets after they move off the screen, a Player object, and a “Player.cs” script for controlling player movement and bullet firing (at least for now). We’ve got these objects up and running pretty quickly but we noticed, as you can see in the image above, that we can fire the bullets constantly. So, let’s add a cool down feature…

Let’s start by noting how long we want to fire before the player isn’t able to fire again. Every 0.5 seconds should be good so we can make a float variable called _fireRate.

Fun Notes: We use “_” before variable names to denote a private variable. [SerializeField] is also a feature you can add before a variable line of “private” variables. This both protects the variable (as it is private) from being changed by other scripts but, also gives designers access to change the float number in the Unity “Inspector” window. See how we can change this float to 0.15 here?

Serialized Fields adds flexiblity

Anyways, back to the cool down code… Now that we have our _fireRate variable, let’s add another variable called _canFire to check against the real time in the game and allow us to understand when we can allow the player to fire again.

These variables will make more sense as we add the conditional statement in our Update(); function. Look at our if statement and just focus on the 2 cool down related sections.

Instantiating the laser object is already done by checking to see if we’re pressing the spacebar. If true, let’s instantiate a _laserPrefab. Now, to check of the cool down condition is also true, we use the && characters. Let’s focus on the 2 highlighted sections from above.

Time.time as Unity describes this as “… the amount of time in seconds that the application has been running for.”

Therefore, we are asking if the game time is greater than the _canFire time, change the _canFire variable to the game time PLUS the _fireRate variable. This setup allows us to update the _canFire variable as the game time progresses and thusly turn on and off the ability for the player to fire the laser.

Let’s see a few examples of 1 second, 1.1 seconds, and 1.6 seconds. Remember our fire rate is every 0.5 seconds to allow the player to be able to fire…

Now, the player is only allowed to fire every 0.5 seconds and not blast away all the enemies too quickly. That is all for now. Let me know how you do your cool down system. I would love see other alternatives.

Until next time, stay cool.

--

--

Wesley Adams
Wesley Adams

Written by Wesley Adams

Passionate animator and game developer; Wesley is a co-founder for Multivarious Games and a Creative Director in elearning at Xcelerate Media.

No responses yet