Creating an ammo system for 2D shooter game in Unity

Austin Young
4 min readAug 14, 2023

--

Goal: Create ammo system for player. Start with 15 ammo and visually show ammo depletion and give feedback when out of ammo.

To start I will approach this as I did my last feature, by first implementing the functionality and then adding the accompanying visuals and sound. For this I’ll need to program an ammo count system and then create a UI text to update my ammo like my score does, and then a sound effect to play when the player has no ammo but tries to hit the fire button anyway.

For this feature all I really need is a variable for ammo count and then to adjust my existing firing method. So I created an integer variable named _ammoCount set it to 15 and also set it equal to 15 in start so it resets back on replay. Now I just need to subtract 1 from that variable each time the FireLaser() method is called, inside the method.

_ammoCount -=1 is same as _ammoCount = _ammCount minus 1.

Now I had to adjust the conditions for when the spacebar key was pushed to account for the ammo. I added && _ammoCount > 0 to the if statement conditions so that it made sure we had more than 0 ammo before we fired. Then added an else if statement asking for the same conditions but if _ammoCount == 0. This way we can play an audio clip here if the player hits spacebar to fire but has no ammo. You can use Debug.Log(“Out of ammo”) here to test for now.

I added [SerializedField] before my variable to check in the inspector during play test that it was working properly and now I just needed to add visuals and audio.

For the UI, I created a new TextMeshPro text and changed it’s text and position the way I wanted using the inspector and scene view. I named it Ammo_text and now I need to have my script update the ammo count in real time.

In my UIManager script I created a new TMP_Text variable named _ammoText and assigned it to the UI text in the inspector. This will allow me to call that text UI object and make changes. Again in my Start method, I set _ammoText.text = “Ammo: 15”; So that it shows the starting ammo when the game starts.

I created a new public method named UpdateAmmo(), and inside the method I set the text = “Ammo: added to the integer parameter variable ammoCount. Which I then set as the parameter for UpdateAmmo(int ammoCount) as such. This way I can pass the player’s _ammoCount variable inside and that’s what the UI text will use.

So back inside the player script and FireLaser() method, I just need to call that UpdateAmmo() method right after I subtract 1 ammo and pass the updated _ammoCount to the UIManager. This will cause the text to update every time FireLaser() is called and right after 1 is subtracted from the ammo count.

Now to add the sound effect, I found a nice 8-bit error sound off Freesound.org and downloaded that real quick and dragged it into my Audio folder in Unity. I then created a [SerializedField] AudioClip variable in my player script named _outOfAmmoClip which I then saved and assigned in the inspector with that new sound effect.

Inside the else if statement I made earlier, I deleted the Debug.Log line and assigned my new audio clip to the audio source and then just called the audio source handle again to play that sound clip. I made sure my laser sound effect clip was being assigned right before it was asked to play inside the FireLaser() method.

Now my player is able to shoot 15 lasers and once ammo shown is 0, a nice little error message will play telling the player they are out of ammo. Now I need to add a way to replenish ammo next.

--

--

Austin Young

o/ I'm a game dev documenting my journey. Mostly technical tutorials. Currently focusing on the Unity engine. My Portfolio: ajamesgames.wixsite.com/website-83