More Ammo!

Refilling Player Ammo

Christopher West
4 min readJun 29, 2022

--

Photo by Velizar Ivanov on Unsplash

Welcome back! I’ll be continuing to implement the requirements for the first phase of the 2D games certification in the GameDevHQ Professional Unity Developer Program! Todays requirement is to create a new collectible object for the player that will allow them to refill their ammo. The requirement reads as follows:

Tasks:

  • Create a powerup that refills the ammo count allowing the player to fire again

Create and Import the sprite assets

Creating a new powerup required some new graphics to be created! I took the existing sprites for the shields powerup into photoshop, changed the text to read “AMMO” and shifted the colors with a filter. Then I re-exported them and imported them into my project under their own folder.

Create the Ammo Collectible Game Object

With the new sprites brought into the project, I need to animate them. By selecting all of the imported sprites and dragging them into my hierarchy window in the project Unity will automatically create a new animation and ask us to name it and save it. It then drops a game object with that animation attached into the hierarchy window! Super simple and effective!

Now that I have my object for my new power up in the hierarchy I can attach the rest of the components I need to it. I add a CircleCollider2D and a Rigidbody2D so that I can handle the trigger collisions needed for the player to pick up the object.

Add a new type value to the PowerupType enum and an effect duration to the Powerup script

Next I need to add a new value to the PowerupType enum so that I can set the new GameObject to be identified as an Ammo collectible when I add the powerup script in the next step. I also added a new variable to the Powerup class to define the duration of a specific Powerup’s effect. This will allow for Powerups to wear off after a set amount of time.

Set the new properties on the ammo power up object in Unity

Next, I add the Powerup script to the object and set the power up type to the newly added “Ammo” powerup type. I also set the Powerup Duration to 0 seconds since this powerup’s effect is instant.

At this point, I prefab the object by dragging it into my Resources > Prefabs > Powerups folder. This folder was added during the cleanup phase that happened before I started the Phase I requirements work and allows the objects in it to be available for me to index dynamically via the SpawnManager script when the game loads.

Player script modifications

With my object now setup and prefabbed I need to add a clause to the switch statement that determines which powerup the player has just picked up. This section of code resets the current ammo count to the designer set maximum ammo, calls out to the game manager to have the UI updated, and plays the pickup sound.

I also modify the TripleShot and SpeedBoost coroutines to accept in the new duration parameter that I added to the Powerup script. and set the duration of each of those powerups to 5 seconds in their respective prefabs.

Next Time!

In this article we added a new powerup item for the player to pick up that refills their ammo. Next time, we’re going to add a collectible that restores player health! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

--

--

Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.