Daily progress: Day 1, Ammo Count.

Adding in an Ammo Count.

With GameDevHQ, we have been set a task to make this 2D space shooter. At this point in the journey, we have to research how to do a list of tasks to add to this game.

Ammo Count:

First, I needed somewhere to store that ammo count. Adding an int variable to start for the ammo count and setting it to 15, we can change this later if it doesn’t seem enough.

Now to check if we have ammo to be able to fire since we are already checking for if we can fire, we can just add it on the end. Check if the Ammo Count is more than 0.

Easy enough so far. Next, I wanted to take away ammo each time the Shoot method was called, simple enough. Adding in _ammoCount — ; at the end of the Shoot method works for this.

The last thing to do is make a visual representation for the Ammo Count on the screen. This can be done by adding a new Text element to our canvas, just like we did for the Score Text. Renaming the default Text to “Ammo: 15” So we can get the sizing right, along with the placement.

Final result for the Ammo text.

With that done, we can open the UIManagers script to add in the functionality.

Add a reference to the Text for the Ammo.

Next, we want to make a method that will change the text when the Ammo Count drops. Again this is almost exactly the same as the UpdateScore method we made a while ago.

As you can see, very similar.

Now to call this method just after we take the Ammo away when the Shoot method is called (back on the Player script)

Now it should all be set up and ready to test the end product.

Works perfectly.

Bonus feature:

Having a number to see that you are out of ammo is all good and well but having to take your attention away from the game to see that you are out of ammo, well I felt I needed to build on this. I decided to add in some audio of an empty gun firing when you try to fire with no ammo. After looking through all the audio clips I have on hand I found one that sounded right for the game.

Now to add the game sound in when you are out of ammo which turned out to be easier than expected. Adding a reference for the new clip and dragging the clip into that reference in the hierarchy and most of it’s done.

Now to check if the ammo is out and if it is change the clip sound to the empty ammo sound.

Lastly, needed to change the clip back if we get ammo back, which we can just put at the top of the Shoot Method for firing.

And done! After a playtest, we can see it’s all working as intended.

Another feature down, Next time I will be adding an Ammo Pickup, See you there!

Game Developer in the making~