Implementing Limited Ammo Feature in our 2D Galaxy Shooter Game in Unity — Part 02!

Objective: To add a visual and audio cue for our player to tell them that they have run out of ammunition.

Let’s jump straight into this with the audio cue first, which we are going to create AudioClip variables in the ‘Player’ script and we are going to drag and drop these in the Inspector and assign them to the audio source component through code at the appropriate time. Let’s get started!

Audio

Previously on the player game object we only had one audio clip that was assigned to the audio source component and that audio clip was attached to a separate game object within the Hierarchy. Instead of creating a new game object and adding this audio clip to it we are going to create it the same way as we did with the thruster gauge audio.

Within the ‘Player’ script let’s add our AudioClip variables for both the laser shot and the empty ammo. We now need to remove the GameObject.Find line as we are no longer looking for a separate game object that holds our audio. Instead, let’s simply grab the audio source component on the player and complete the usual null check.

While we have just created these variables, let’s head over to Unity and drag and drop the audio clips into the Inspector window:

Now within our Update() method, we just need to add one line of code which is going to assign the specific audio clip that we want to play which in this case is going to be the _emptyAmmoClip:

Lastly for our audio, let’s jump into the FireLaser() method and assign the _laserShotClip audio clip:

Audio sorted!

UI Element (visual)

Let’s keep this one nice and simple and have some text that shows on the screen saying “OUT OF AMMO”. As this is going to be a UI feature, let’s let our ‘UIManager’ script handle this, which we will jump into shortly, but first:

On the Canvas in the Hierarchy window let’s create a new Text game object → Right click on Canvas → UI → Text.
Rename: ‘Out_Of_Ammo_Text’.
Anchor: Bottom middle and change the PosY to 20.
Width: 230
Height: 35
Text Box: “OUT OF AMMO”
Font Style: Bold
Font Size: 30
Alignment: Centre and Centre

Let’s add some code to the UIManager script

Firstly our serialized Text game object, so that we can drag and drop this game object in the Inspector. We are also going to need a switch that will check to see if our coroutine is already playing. If it is, it will do nothing.

Now we need to create a new public method OutOfAmmo() that is going to be called by the player script when they are trying to press space and shoot a laser with 0 ammo.

And finally, our coroutine that is going to handle the _outOfAmmo text game object flicking on and off.

Later on I plan to add a better UI feature that is going to show the current and maximum ammo near the thruster gauge!

--

--

--

Passionate Unity Game Developer

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

Recommended from Medium

How To Delete Unwanted App In Mac

How I’ve optimized my first VR game prototype on Unity

Creating an Elevator in Unity

5 Reason to love Serverless Architecture

Guidelines to Learn a Programming Language

How to effectively use the Interswitch API documentation.

Headless CMS In Flutter

The True Value of Software

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
Chris Hilton

Chris Hilton

Passionate Unity Game Developer

More from Medium

2D Mobile: Loot System

Instantiate and Destroy Game Objects in Unity

Understanding the Body Settings in Cinemachine Virtual Cameras

Galaxy Shooter 2D — Super Laser