Adding a collectible ammo charge powerup
Let’s create a new game object for our ammo charge collectible.
We’ve run out of pre-made assets from GameDevHQ at this point so I’ll be using placeholder icon sprites from an asset called “Clean Flat Icons” that I’ve got lying around so to speak.
Clean Flat Icons | 2D Icons | Unity Asset Store
Elevate your workflow with the Clean Flat Icons asset from RainbowArt. Browse more 2D GUI on the Unity Asset Store.
We’ll start by creating game objects for our ammo sprite and charge sprite which will be combined to be our Ammo Charge Powerup.
After playing around for a bit with the settings, I came up with these.
For the Ammo_img we want to give it a Z rotation of -45 and set our sorting layer to Foreground.
On the Charge_img game object I changed the position and scale, color, and set sorting layer to Foreground and Order in Layer to 1.
It’s important to set the Order in Layer to 1 so that it will always appear in front of the Ammo_Img game object.
I’d suggest keeping the position as 0,0,0 until you have childed this game object to our Ammo_Charge_Powerup game object we will be creating next.
As promised, we create the Ammo_Charge_Powerup game object and child our image objects to it.
We also add a few components for gameplay purposes.
If we look at our existing powerups we see there are a few more components we need to add.
We need to add the powerup script and audio source components.
We won’t be animating this placeholder so no need for the Animator component.
After that, go ahead and drag the Ammo_Charge_Powerup game object into the Powerups folder of our Project view to create a prefab.
Delete the Ammo_Charge_Powerup game object from the hierarchy view.
NOTE: In the gif above, I assigned the power_up_sound audio clip directly to the audio source component.
Clear this out and add it to the “Powerup Audio Clip” variable on the Powerup script component of our Ammo_Charge_Powerup prefab game object.
Modifying our Player script
Our goal is to create the logic that when the ammo_charge_powerup game object is collided with, we will refill our player’s ammo charge back to full (15 shots).
We add a RefillAmmoCharge public method to our player script, set our variables to their full values, and modify the ammo charge indicator game object to update on the player screen.
Modifying our Powerup script
The first thing I want to do is update my comment for the _powerupID variable to include a choice, in this case 3, for the Ammo Recharge powerup.
Then we’ll update our OnTriggerEnter2D() method with another case statement for a value of 3 and call the RefillAmmoCharge() public method we created on our player script.
I also need to update the part of this method that disables the SpriteRenderer components.
Our original powerups have the SpriteRenderer component directly on their Game Object.
Our new ammo recharge game object has no SpriteRenderer component, but it has 2 child game objects, which is where the SpriteRenderer components reside.
So we can know the powerups which will run find with current code are all ID number less than 3 (0, 1, and 2).
A simple if statement implements the choice logic and we use our else statement for IDs above 2 to create a SpriteRenderer array where we store all of the child game objects’ SpriteRenderers.
We run through each of these using a foreach statement and disable the SpriteRenderers one by one for however many there may be.
If in the future, even if we have a powerup with 3 or more child game objects and SpriteRenderers, our new else code will still run correctly.
Before we forget, let’s set our PowerupID variable on our Ammo_Charge_Powerup prefab game object’s powerup script component.
Modifying our SpawnManager script
First we’ll need a variable to hold our new prefab game object for the Ammo recharge.
Save the script, return to the Unity editor, and assign the Ammo_Charge_Powerup prefab game object to the SpawnManager script on our SpawnManager game object.
Next, we add a new coroutine, SpawnAmmoChargeRoutine() as shown above based on previous coroutines.
Now we can call this new coroutine in our StartSpawning() method as shown above.
That’s it, give it a try!