Reload! Adding Ammo Powerup Functionality
Our GameDevHQ journey continues with the addition of a custom game collectible. In this first installment, we’re giving our Player the chance to grab more lasers to defend against the onslaught of Enemies.
I wanted to first try something simple, like using a ready-made asset from Filebase. However, I couldn't find anything I liked, or fit the current aesthetic of my game. So, after much inner turmoil regarding trying to keep my designer side out of the mix to focus on my programming, I finally gave in to my inner creative self and set to work.
I was able to quickly create a small animated GIF using an old asset I’ve had for years, which consisted of a rotating cylinder of green light. I imported this small animation into a freeware program called ScreenToGIF (https://www.screentogif.com/). It has all sorts of great tools available for handling animated GIFs, and definitely worth a download and donate!
I was able to add the text using this program so that Users know what this new thing was falling towards their Player in the game. Another nice thing about this application is that I had all the frames nicely separated and available to import into Unity.
So, now that the graphic design gushing is out of the way, let’s return to Unity. I added a new subfolder to the Power_Ups Sprites named “Ammo” to hold the relevant frames of the Ammo Sprite.
Next, I created a new, empty 2D Sprite within the Game Hierarchy to be a placeholder for the animation.
We’ve set up animations in the past, but as a refresher, we’ll take the first frame of the Ammo Sprite and drag it into the Sprite definition of our new GameObject so that Unity knows what to display. We’ll name the GameObject Ammo_Powerup.
Now that we’ve set up the initial Sprite and adjusted its size as needed, we’ll head over to the Animation tab and create the Ammo Powerup animation.
We’ll want to attach the relevant components as we did with our other Powerups. In this case, the Rigidbody 2D and Collider 2D components with the proper settings so that it’s triggered and falls properly. Since this is a Powerup, we’ll want to attach the Powerup script to the animation as well.
Get Stephanie Wells’s stories in your inbox
Join Medium for free to get updates from this writer.
Our Powerups spawn using the Spawn_Manager. We’ll increase the size of the Powerups from “3” to “4” to accommodate the new Ammo Powerup.
We’ll make our new Ammo_Powerup GameObject into a Prefab that we can then drag into the new Powerup element we’ve created.
Our Spawn_Manager should now have 4 Powerup elements, with our Ammo_Powerup Prefab being assigned to Element 3.
We’ll want to ensure we assign the proper PowerupID to our Ammo_Powerup Prefab so that our script works as expected.
Now, on to the scripting! We’ll edit our SpawnManager.cs file to increase the Random Range of Powerups from 3 to 4 to include our new Ammo Powerup.
Next, we’ll add a new case to our switch statement. While we’re testing, we’ll use the Debug.Log to see if our Game recognizes our Player collects the Ammo before moving forward. This, of course, will be commented out once we get everything set up.
We’ll hop over to the Player script and create an AmmoCollected method. This method serves to add a specific amount of Lasers to our Laser count. It will also recognize there is a cap on how much Ammo our Player can have. Thirdly, it updates the value of the Ammo Count in the UI.
The AmmoCap method checks to see the value of the Ammo count. If the count reaches more than 100, the game resets it to 100. Admittedly, my inner child got the best of me at the Ammo Cap. I set it so that the Player gets a LOT of Lasers with this type of cap. But, that could be great for longer gameplay, and just having fun, right?!
After a brief playtest, we’ll make sure we return to our switch statement to ensure that we make use of the code we created within the Player script.
We can now collect Ammo Powerups and continue fighting Enemies in our Shooter!






