2D Unity Game Dev — Day 3 — Cooling down them barrels!

Objective: Find a way to regulate the rate of fire of a weapon, or similarly, simulate a cool-down period of a weapon before allowing the next shot. This builds on my previous article found at:

Up until now, we’ve used primitives to build a rough prototype of some sort of shooting game. We have the Player(a cube) that can move in a limited range vertically, and wrap around the screen horizontally, using the ASDW or arrow keys. We were also able to create projectiles (small capsules), save them as prefabs, and fire them vertically upwards using the SPACE key. These same projectiles destroy themselves upon reaching an upper limit outside the game view to avoid collecting garbage the longer the game runs.

Now our next objective is to limit the rate of fire of the Player. Why is that important? Well, first of all, it isn’t realistic to have a weapon without some sort of limitation. It could have limited ammo, it could require time to cool down if it’s some sort of energy weapon, or it could simply be a limitation of the mechanics of the weapon such as the time it takes for a spent round to be ejected and a fresh round to be seated in the chamber.

As with all of these little challenges or behaviors we try to implement, they can be approached in different ways. I’ve opted to use a coroutine that will shut down (via a variable of type bool) the Player’s ability to fire his weapon for a fixed period set by a variable of type float. So let’s setup those two variables as depicted below:

Private bool to turn off the weapon, and private float to set the rate of fire.

We then modify our Update() method to look for the depression of the SPACE key AND to verify that the player’s weapon has cooled down. By default, this would be TRUE, but as you will see below, once the laser has been instantiated, this bool value will be set to FALSE preventing the further firing of the weapon.

IF statement looking for both the SPACE key pressed down (True) AND the Player’s weapon being ‘cold’ (True).

If both statements are TRUE, then we Instantiate the laser prefab at the Player’s position, flip the _hasPlayerLaserCooledDown bool to FALSE, and start the weapon cool-down coroutine PlayerLaserCoolDownTimer().

The coroutine is basically there just to set the _hasPlayerLaserCooledDown bool back to TRUE. The illusion of a cool-down is created by suspending the coroutine through the use of WaitForSeconds. Instead of having the time hardcoded to a set value, this is where I’ve used the _playerRateOfFire float.

I’ve done this because I wish to have the option of amending the length of time for which the Player loses his ability to use his weapon as a result of damage taken or interaction with enemy GameObjects in future gameplay. With my current experience, I would think that manipulating a single variable would make the task much easier to handle 🤔.

Coroutine sets the bool back to TRUE after waiting for a period of time as set by a float variable.

So there you have it. We now have a movable Player that fires off ‘laser shots’ with a realistic rate of fire. Join me in my next few articles as we tackle the challenges of implementing adversaries into our prototype game.

Updated Player script.

Military member with 34+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Funny commands in Linux

Night Owl’s Weekly Dev Update [13/04/2022]

How To Create an App Using Django Framework -Tutorial

📢News of the Week

An introduction to HelloFresh’s SCM Engineering

Microsoft Office and keeping backwards compatibility in mind


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michel Besnard

Michel Besnard

Military member with 34+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.

More from Medium

Instantiate and Destroy Game Objects in Unity

Galaxy Shooter 2D — LookAt (2D)

Platformer: Death Animation

Unity Performance Optimization: Animation Module