Galaxy Shooter — Ammo Count

Greetings, and welcome to this special series of articles. In this series, the intention is to take a solid functional base game and add some extra features that (hopefully) make it just a little more special.
The base game we have here is called Galaxy Shooter. It’s a Galaga-style 2D shmup that I put together while going through the 2D Game Development course for GameDevHQ. It has a controllable player ship, enemies, powerups, sound effects, music, and more. It is, as stated above, a fully functional game that can already be played and enjoyed in its current state.
But let’s see what else we can make it do.
In this article, we’re going to limit the player a bit. Previously, there was no penalty for simply firing wildly at all times. With the upcoming changes, the player’s number of shots (or ammo) will be limited.
We will be giving the player something as well. Because the ammo will be limited, we are going to make sure the player always knows where they stand in regards to their capabilities. With that in mind, we are going to build a meter in the UI displaying how much ammo is available. We’re also going to make sure the player knows when they are out of ammo by giving them audio feedback.
Let’s get started.
How things work initially
The player’s “laser” initially has very few limitations to it. It fires once with each press of the fire key and has a short “cooldown” period between shots. Aside from that, there is nothing keeping the player from firing as much as they want.
When the player fires, a laser object is instantiated and a sound effect is played. This laser object immediately begins moving in a straight line. Certain objects (enemies, primarily) are scripted to react when colliding with the laser object, but if that does not occur the object will destroy itself once it reaches a certain distance.
Also, there is a “triple shot” powerup available to the player. This powerup changes the object instantiated into a three laser composite object. It effectively gives the player more area coverage with their fire.

Limiting the player’s ammo
Limiting the player’s ability to fire is not too complicated. The first thing we will want is two new script level variables to keep track of the player’s current amount of ammo and their maximum amount of ammo. We could get by with just the current ammo count, but something tells me we’re going to want to reference back to the maximum in the future.
For the maximum variable, make sure to assign it a value (in this example, the maximum ammo will be 15). It would also be helpful to serialize this field so it can be easily changed. For the current level variable, make sure to assign it the value of the maximum in the Start method.
With those two variables in place, we only need to make two changes to the FireLaser method. The first is to add another conditional to the if statement checking if the current ammo is above zero, and the other is to decrement the current ammo when the player successfully fires.

If we run the game now, we can see for ourselves that the player can only fire the specified number of shots before they become incapable of firing more.
I will note though that the triple shot powerup was not changed to consume three of the player’s ammo level. That struck me as being against the spirit of a powerup.
Showing the player their ammo level
The next step in this update is to show the player what their ammo level is currently. For this, we’re going to build a meter into the game’s UI that will represent the player’s current ammo level.
For this I created a simple new sprite to become the meter. It’s basically just a rectangle with rounded corners. It is all white so that the color can be adjusted easily from within Unity, and the format is in PNG to have a transparent background.

With the meter sprite imported into the Unity project, I then created a new empty object in the UI to hold everything the ammo meter needs. What the ammo meter needs is two UI image objects — one for the background and one for the foreground. The meter sprite will be used for both.
The colors for both the background and foreground are set to whatever is desired — I went with an orange on dark gray color scheme. Then using the Unity Editor, move and scale the components of the meter until it looks correct.
An important thing to do during this stage is to change the Image Type of the foreground object to be Filled, and then change the Fill Method to Vertical. This is how we are going to have our meter shrink.

With the UI elements created and positioned as desired, the next stage is to update the script so that the UI will change appropriately from player action. The game already has a script that handles all UI elements, such as the player lives and score. We’ll now add some code to this script to handle player ammunition count.
Create a new public method that takes two parameters — the current ammo and the max ammo. We will divide the current ammo by the max ammo to get the percentage of ammo remaining, which will then act as the fill level for the foreground meter object.

This method needs to be public because it will need to be called from the script that handles player fire. The variable _ammoMeter references the meter foreground object.

Success! Only one thing left to do now, which is get the player’s attention when they run out of ammo.
Letting the player hear that they have run out
The final feature we want to add is an audio cue for the player when they attempt to fire with no ammo. This should be a simple matter, as we are already playing a sound for a successful fire. But we don’t want to get careless now. There are some issues we need to be mindful of if we want this feature to work flawlessly.
Naturally, the first thing we need to do is create a new serialized variable to hold our reference to the new sound clip. Once that is done, all the remaining work lies in the FireLaser method.
One of the tricky issues to keep in mind here is that we need to shift our conditional statements around a bit. Previously we had it so that we need the user to be pressing the space key AND the cooldown needed to be reached AND the player needed more than zero ammo. For this new feature, we still need the first two conditions to be met, but we need the opposite of the last.
So we can start by taking the ammo requirement out of this if statement and creating a new if statement inside the old. But now we see tricky issue number two, in that we probably still want the cooldown updated even if the player fails to fire. Move that line of code outside the new conditional.
Finally, we create an else statement to handle our player misfire condition. This block of code just sets our audio clip and then plays it.

And that finishes things up! In the next article, we’re going to expand the game powerups to include an ammo resupply, because the player is going to want to be able to keep shooting! Until then, good luck and happy coding!