There must be limits!

Limit Player Ammo

Christopher West
5 min readJun 28, 2022
Photo by Clint Patterson on Unsplash

In todays article we continue implementing the requirements for the first phase of the 2D games certification in the GameDevHQ Professional Unity Developer Program! Todays requirement is to limit the number of normal shots the player can fire by adding an ammo limit. We’re also going to alert the player that they have reached their ammo limit with an audio sound. The requirement reads as follows:

Tasks:

  • Limit the lasers fired by the player to only 15 shots.
  • When the player is out of ammo, provide feedback through on-screen elements or sound effects. (ie: beep or ammo count displayed on screen)

Add the Ammo Counter UI Elements

I plan to add both an ammo counter and the audible sound effect for when the player is out of ammo. With that in mind I like to start with UI when I know I'll need it up front. For this requirement, I decided to add the ammo counter Below the player lives display but above the shields. I started by moving the shields display element down in the UI to make space for the new ammo UI elements. Then, I added a new empty parent object to hold the ammo UI elements. After that, I added two text elements inside the parent element. One for the label with the static text “AMMO:‘’ and the other to hold the value of the ammo count. I hope to expand the ammo count beyond the required 15 shots in a future iteration of the game, so I set the default text to present as “99999” which allows me to adjust sizing and spacing for a larger value that may exist in the future. Once the text elements are in place I adjust their anchors and positions to allow for the spacing to expand evenly if the width of the parent element is adjusted in the future.

Adding the ability to update the ammo count text

Now that I have UI elements I need the ability to update those elements. I’ll need a reference to the UI text element for the Ammo Count, so I create a Serialized Private Field, allowing for this to be set in the designer. After this, I’ll add a method to my UIManager class called UpdateAmmoCount that looks like:

I also make sure to assign the Text field for ammo count to the UIManger in the editor

Adding the UpdateAmmoCount method to the GameManager

Next, I add the UpdateAmmoCount method to the GameManager class and set it to call the UIManager class’ UpdateAmmoCount method. adding this method allows the for calls to the UI to be managed in a central location with other shared communications.

Add the audio clip

With UI and commmunication with the UI setup, I know that I’m going to need an audio clip to play when the player runs out of ammo so, before I add changes to the player class, I import the audio file supplied with the assignment into my project from FileBase.

Changes to the Player class

With all of my UI and audio setup it’s time to add the feature changes to my Player class. I start by adding fields for both the current ammo count and the maximum number of shots the player can have. I make the latter customizable from the editor by using the SerializeField attribute.

I also added a field to keep track of the audio clip for the ammo depleted sound and made it assignable from the editor.

I also make sure to assign the Audio clip in the editor.

With those fields in place, I initialize the ammo count, check for the existince of the ammo depleted sound, and update the ui with the players starting ammo count.

Finally, I modify the Player classes OnFire method to include checks for the ammo count, reduction of ammo if it’s not out, a call to the GameManager to update the player’s ammo count, and playing of the the depleted sound if the ammo is out.

With these changes our new modification should now be in place!

Next Time!

In this article, we successfully limited the number of shots our player has and provided visual and audible indicators for when they run out of ammo. Next time, I’ll add a new collectible to the game so that the player can refill their ammo! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

--

--

Christopher West
Christopher West

Written by Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

No responses yet