Adding Features: Ammo Refill
You may have noticed that the graphics and Visual effects have changed over time. That is a natural process is development. Don’t worry, we will go over everything in time.
For now, lest talk about the Ammo situation. We have added the counting system and limited our starting ammo count to 15. Let’s honest, that is not a lot of ammo when you are trying to defend the galaxy.
In a previous article, we created an array and filled it with our current powerups. Let’s add ammo to the list.
First thing I did was search out the visual representation of my powerup.
I found these graphics in the Unity Asset Store, and I paid somewhere around $6 for them. There are plenty of free options out there, I just happened to like this one.
Once you have downloaded the assets you want, you need to import them into Unity.
If you used an asset pack from the Unity Asset Store, then you can access that pack from your package manager.
Open the Unity Editor, at the top select Window and scroll down to package manager.
In the package manager window, at the top, select the dropdown for Packages, and select ‘my assets’. Everything you have downloaded from Unity will be here.
Select the asset pack and in the bottom right corner, select import. There should now be a new folder inside your assets folder.
Firs we need an object for our powerup. Open your newly downloaded assets, pick the one you want, and drag it into the Hierarchy tab. The object will appear on the screen.
Rename it and resize it if necessary.
The next step is to make a prefab. To do that, simply drag your object into the prefabs folder.
Attach the powerup script to the prefab by dragging it onto the object, or with the ammo prefab selected, drag the script into the inspector. You will also need a rigid body 2D and a collider 2D. I chose the box collider because it fit my object.
Our Spawn Manager controls the spawning of our powerups, based on their powerup ID. To assign the powerup ID, Select the Spawn Manager object in the Hierarchy. In the inspector, you will see the current list of powerups in the script component.
click the plus button to add an empty slot. Drag your ammo prefab into the empty spot.
In our spawn manager script, we have the powerup spawn routine.
We need to change the random range from (0, 3) to (0, 4). Remember, the first number is inclusive, while the second number is exclusive.
With a random range of (0, 4), the range will go from zero to three. If you look back on our elements in the inspector, you will see that our powerups are in elements from zero to three.
Now our ammo powerup will spawn. But how do we make it work. Since the powerup will be colliding with the player, let’s go to the player script and build some logic for the ammo.
The first thing I did was create the ammo up variable. I used the serialize field attribute so that I can manipulate it from the inspector.
Next, I created the ammo up method. If the method is called, we will add the value of my ammo up variable to my ammo count.
I wanted the ammo to max out at 99, mostly for aesthetic purposes. To do that, I used if-else logic.
After the ammo count is updated we check to see what the new count will be. If the count is greater than or equal to 99, we set the count to 99 and update the UI manager. This will cause the UI manager to set the display to 99.
If the count is not greater than or equal to 99, we simply update the UI manager with the new count.
Our powerup script is where we check for collision with the player. To add the ammo powerup I went to the switch statement and added another case (case 3) that corresponds with the element reference for the ammo powerup. In that case I called the ammo up method from the player script.
Verify that “Is Trigger” is checked on the collider component of your ammo powerup.
To test that everything is working properly, I went to my player and set the ammo up variable to 100. I used 100 to verify that the ammo will cap at 99 and not display anything over that.
When I start the game my ammo count will be at 15.I added a powerup to the scene so I didn’t have to wait for one to spawn randomly. If everything works correctly, my ammo count should start at 15 and go to 99 when I pick up the power up. then when I shoot it should decrease from 99.