Ammo Reload Power Up
In the last article, we added an Ammo counter which tracks the number of shots the player has left and when the Ammo is out, they can no longer fire. Leaving the Player stranded without a way to get more is not cool, so we will add an ammo refill to do reload the Ammo supply.
The first thing to do is create an Ammo PowerUp prefab. This is an empty 2D GameObject and I am currently using the Triple Shot sprite image as a place holder. A Circle Collider 2D and Rigidbody 2D components will be added, so it will be able to decide collision and have physics applied to the GameObject. I have already have a modular PowerUp script made, so I attached it to the Ammo Reload Power Up. The beauty of this modular script is its ability to easily add more Power Ups with it and not have to write a new script for each one. I just need to give it a new Power UP ID number.
We need to update the PowerUp script, so in the OnTrigger method when the Player collides with the a PowerUp, it checks the PowerUp ID of the PowerUp using a switch case. Since we gave the Ammo Reload a PowerUp ID of 3, we need to add Case 3 that will call the Ammo Reload method from the Player.
Jumping into the Player script, we will create a public method that will handle this AmmoReload. In this method, we set the current ammo to the max ammo and set the has ammo boolean to true.
There is a slight issue when the player Ammo hits 0 the text turns red, but when they get the Reload Power Up the count changes back to max ammo with changing the color of the text back to white. In the UIManager script, we will just use an IF ELSE to check to see the current amount of ammo and depending on the amount will determine the color of the text.
Right now, everything is setup for the Ammo Power Up to work but it will never appear in the game because we have a Spawn Manager script handle what Power Up will spawn. This is handled be a random number being assign to a PowerUp variable and depending on the number will determine the PowerUp to spawn in the array. When we only had 3 PowerUps the range was from 0 to 3 (0,1,2). This range is exclusive so the 3 is not included in the range. We need to extend the range from 0 to 4 (0,1,2,3) to include the new PowerUp. Now, the Ammo Power Up is spawning with the rest and reloads the Players ammo back to max ammo. The Player needs to be less trigger happy than they were before.