Limiting your spaceship ammo and giving the player the info on screen via a HUD.
Today's challenge was to limit the player's ammo to a limited amount of shots and when they ran out, to play some sort of audible alert as well as provide the information on the screen.
Setting the ammo limit was easy enough. Just create a variable, reduce 1 every time the player fires, and check to see if it reaches zero. If it does, play a sound. So here’s how I implemented it:
I created a public integer variable called ammoCount and immediately set it to 15, which was the challenge number of ammo the player could shoot. The reasoning behind the public instead of private was so I could transfer that information to the UI manager which would need the information to update the numbers on the canvas/screen.
I also created a holder for the buzzer sound which I then dragged the asset into in Unity's inspector.
I decided to reduce the ammo in the FireLaser() method in my Player script:
I wrapped the current entire laser firing in an if/then check basically assuring that if there were more than 0 lasers waiting to be fired, the player can actually fire them, otherwise, it would default to the else statement which switched the audio clip to a buzzer and then played it.
If the player could fire, then the ammunition count is reduced by one, and the UI manager is notified and is commanded to run the UpdateAmmoCount() function.
For the UI section, I added a text component called AmmoText, and an image called Laser_Healthbar. For the image, I recycled the actual laser image, rotated it, and set it as filled type which I’ll explain in just a moment. By now, I’m sure you know how to do this, so you can skip the next image that does it step by step if you like.
So why did I change the image to filled? there’s a slider called fill amount which affects the image very much like a health bar does. Watch!
Ok, that’s cool and all but not what we need, fortunately there’s other wipe methods, there happens to be one called vertical, let’s see what that one does…
PERFECT!!! Now all we need to do is rotate and size the laser to a decent healthbar’ish looking size…
Now we’ll head on over to our UIManager script.
First we’ll create some holders for our ammo UI elements as well as variables for our ammo count. Max Ammo is basically the same number as ammo count at the beginning, but we’ll need it for the math necessary for the health bar.
In the Awake() function (Which gets run before Start, so it gives you an even earlier method to initialize things which is useful for GameObject holders and the like) I initialized _maxAmmo to equal the _ammoCount variable which actually goes into the player script and takes the value from there. We also tell unity to find the ammos text obect with the GameObject.Find() command. This helps us avoid clutter in the inspector by avoiding dragging and dropping the item ourselves, we’ll be doing more of this in the future.
In the Start() function, we actually initialize the ammo and score via their functions. Let’s go to the ammo count function.
This function is public because not only do we initialize our ammo count at the beginning, the player script will access this script every time a bullet is fired so the UI is always updated! It keeps everything nice, separated, and organized. Once initialized, _ammoCount grabs the player scripts ammoCount information and stores it into itself. Then the Text is updated to show the ammoCount, and the ‘health bar’ is updated by dividing the amount of ammo into the maximum amount of ammo giving us a fraction between 0 and 1 which is exactly what the Image Fill function needs to show a proper health bar level. The reason for the (float) is I had an issue before where the health bar would disappear after the first shot! It’s because the health bar needs a floating number while the ammo amounts are integers. Once recast into floats, the algorithm worked perfectly!
Tomorrows challenge will be to create a powerup that restores the ammo!