Limited Ammo & UI Ammo Counter
In the previous article, we have buffed the shield power-up to hold three charges. It is only just to add some more limitations to the player. We will do that in the form of a limited ammo supply for the laser.
Objective:
Allow the player to hold a maximum of 15 shots of the laser. If a player runs out of ammunition, play a sound. Update the UI to show the current & maximum ammo of the currently active weapon.
Implementation:
I will start by limiting the player to 15 shots of the laser weapon. For this, I will need two different variables. At the start of the game, I will set the value of ‘_currentAmmo’ to the value of the ‘_maximumAmmo’ variable.
Next, every time the player shoots, I have to check whether they have enough ammunition; if not - I will play a sound clip I have found online, which will signal the player that he has run out of stuff to shoot!
And naturally, in the ‘Fire()’ method, I am decreasing the value of the ‘_currentAmmo’ variable by one with each shot. I am also updating the UI with a new method, called ‘UpdateAmmo’ - we will take a look at that momentarily.
To show the ammo value on the UI, I have created a new GameObject on the ‘Canvas.’ I have populated this GameObject in the ‘UIManager’ and set its starting value to “Ammo: 15 / 15.”
Next, here I have created the ‘UpdateAmmo()’ method. Every time this method gets called, the UI is updated with the current ammunition value.
And that is it! Pretty straightforward. Since we can’t let the player play the whole game with just 15 shots, in the following article, I will be creating a couple of new collectibles, mainly the ‘Ammo Collectible!’
But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!