Sitemap
Stephanie Wells

Stephanie Wells

Reload! Adding Ammo Powerup Functionality

5 min readAug 20, 2024

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.

Press enter or click to view image in full size
Large text of the word Ammo

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.

Animated GIF of Ammo Powerup asset. It consists of a green cylindrical light with a silver top and bottom. The light pulsates and rotates while the word “Ammo” flashes in different colors near the bottom.

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!

Press enter or click to view image in full size
ScreenToGIF Editor mode

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.

Screenshot of Unity Project Herarchy showing the relevant Ammo frames added to the Powerups

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.

Press enter or click to view image in full size
Unity Hierarchy with new Sprite added to the game

Next, I created a new, empty 2D Sprite within the Game Hierarchy to be a placeholder for the animation.

Press enter or click to view image in full size
Screenshot of Unity displaying the moving of the first frame of the Ammo Sprite animation into the Sprite section of the GameObject.

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.

Press enter or click to view image in full size
Screenshot of Unity Animation Tab

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.

Press enter or click to view image in full size
Screenshot of the Ammo Powerup set up in Unity

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.

Press enter or click to view image in full size
Screenshot of Spawn_Manager in Unity

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.

Press enter or click to view image in full size
Screenshot of the new Ammo Powerup being added to Prefabs folder

We’ll make our new Ammo_Powerup GameObject into a Prefab that we can then drag into the new Powerup element we’ve created.

Press enter or click to view image in full size
Screenshot displaying Ammo_Powerup Prefab being dragged to the Spawn_Manager

Our Spawn_Manager should now have 4 Powerup elements, with our Ammo_Powerup Prefab being assigned to Element 3.

Press enter or click to view image in full size
Assigning the PowerupID value to Ammo_Powerup

We’ll want to ensure we assign the proper PowerupID to our Ammo_Powerup Prefab so that our script works as expected.

Spawn Manager script with random range adjustment

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.

SpawnManager script with additional case for 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.

Ammo collected method within Player script

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.

Press enter or click to view image in full size
Screenshot of the AmmoCollected and AmmoCap methods within the Player script

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?!

Edited switch statement

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.

Gameplay showing collecting the Ammo Powerup

We can now collect Ammo Powerups and continue fighting Enemies in our Shooter!

--

--