Phase 1: Max Ammo Count and Collectable
Objective: Set up an ammo limit on the player with a collectable refill system.
For this objective we will set a max ammo of 15 on the player. When the player tries to shoot when the ammo is empty it will play a sound to alert the player and there will be an ammo collectable that fills the maximum 15 count when collected.
We’ll start with the logic for the ammo. In the player script, create a serialized private int for the ammo and initialize it with 15 shots in the start method. Within the update method we already run a check for if the player can fire and if the space key is pressed then call the shoot laser method. We need to change that so that we check if the player has ammo then reduce ammo by 1 and call the shoot laser method.
Next we implement a player ammo ui text element like we did in an earlier post for the score.
Now that we have a working ammo count and UI, the next thing we need to implement is the ammo collectable. We do this the same way we implement the powerup collectables in some earlier posts (set up powerup, add the powerup to the modular system, animate the ammo).
So after we created the animated powerup, made it a prefab, added the power up script, set the powerup id after adding the powerup to the spawn manager script(modular system), calling the new power up and setting the ammo UI text to match the player ammo all that’s left is to play a sound when the ammo is empty.
We had already set our player script to use an audio clip for the laser sound. We can use the same audio source but we will need to add a new serialized audio clip for the empty ammo sound. We can then set the audio source clip to swap between the laser and empty ammo sound just before as only one will be used at a time.
In the next post, we will add a health powerup.