Framework 1: Ammo Count (part 1)
An two part serious on Ammo Pick-up and Limited Ammo Capacity
Time to make the game a tad more engaging and slightly harder by implementing an limited Ammo capacity!
Technically any graphic image is fine as long as it conveys the message across. What you will need is an image or text element (or both) that tells the player how much ammo you currently have, and some user feedback element to tell the user that ammo is out.
On the canvas object, create an new UI image and text element. For me, I will using both an image an text. I will also have the text element be a child of the image just for organizations sake.
UI Manager Script
We are going to be using the UI manager script to host the ammo counter elements.
First we are going to be creating a new text handle for the ammo text. Then we are going to create a new public method called “Update Ammo” with an integer called “player ammo”.
To update the UI, we are going to use the script “_ammoText.text = playAmmo.ToString();” Basically it means the text component is the same as the method integer which is then converted to a string format.
Then to add some nice user feedback, we will be changing the color from white(default color) to red when the ammo counter reaches zero. First we are going to create a new handle containing an sprite renderer. Then using an If statement we are going to check when player ammo reaches zero, we change the color to red. Otherwise the sprite renderer will stay at white(default) color.
Back in the player script, create and Serialized Field private integer handle for the amount of ammo the player is going to start with. In this case I set the total amount to 50 (because I really need that much to survive). Then under the fire laser method, create a new method called “Ammo Count” with an integer call “bullets”. Within the method create an if else statement to cap the player from going beyond the 50 total limit. Then have the else statement contain the ammo count += the integer bullets. Close the If statement and finish off the method with an reference call to the UI manager script calling the Update Ammo method that we create moments ago.
Finally, in the Fire Laser method, before the triple shot if statement call the Ammo Count method with an negative 1 (or how many bullets you wanna use per shot).
Save and return back to Unity and within the Inspector of the Canvas, where we store our UI manager script, drag and drop the Score Text component into the Score text slot.
Once you hit play, the Ammo Counter should count down with every shot you fire!