Space Shooter Challenge: Ammo Collectable

Calum Slee
Level Up Coding
Published in
4 min readMay 27, 2021

--

Our game now has an ammo count, but no way to replenish ammo. Time to expand on the modular powerup system. I started by creating the sprite.

To do so, I opened one of the existing powerups in GIMP. After a quick google search to find a blocky font similar, I created a new text box and scaled it next to the original to match as close as possible. Then I cropped out the original, and dragged the new text in to place.

Creating the white outline took a bit of work. I’m no artist and it was my first time using GIMP. I soon discovered you can add Path to Text which creates an outline path. Creating a new layer then drawing the path allowed me to trial and error and end up with the above image. Can’t tell the difference right?

Lastly, I put all the text colors from the original animated sprites into a palette and then duplicated the Ammo Powerup, changing the text color each time.

It took a bit of work, and a lot of learning, but now I have a template for future powerups.

Back in Unity. I imported the first sprite, renamed it, and rescaled. Then I put it in the Foreground Layer. Added a Box Collider 2D and a Rigidbody 2D then the Powerup Script. I then set the Powerup ID to 3, made it a Prefab, then on my SpawnManager, dragged the Prefab into my Powerups Array.

The ammo powerup will now spawn randomly with the other powerups.

To create functionality, a new Case needs to be added in the switch statement of the Powerup Script.

Initially, there will be an error as the AddAmmo method hasn’t yet been created. I also opted to pass in an int variable _ammoPowerupCount that can be assigned in the Inspector on the Powerup Prefab. This is more modularity for the future, when I may want multiple levels of Ammo Powerups, such as small, medium or large.

I have also now done the same for the shield powerup.

Over in the Player Script, I created the new method accordingly.

The _ammoCount variable already exists. I simply want to append the passed int value on to it. Additionally, I don’t want the user to be able to accumulate too much ammo so I created an int variable for _maxAmmo.

I can then check if the ammo count exceeds the max ammo amount and lock it to that value.

Lastly, I also need to call the UpdateAmmoUI method that is also called when the Laser is fired, so the UIManager can increase the ammo count as well as decrease.

I also adjusted the UpdateAmmoUI method to require a second int variable so I can pass in the max ammo variable.

This allows me to append the max ammo to the ammo total, showing the User how much ammo they have out of the maximum allowed.

I have also added an _ammoTotal variable for the UIManager so I can assign the ammo count to a variable that can be used across other methods of the script. This is so I can stop the Coroutine. The ammo can now reach 0 and start flashing, but then be increased.

To stop the Coroutine, I changed the while loop to run while the ammo total is equal to zero.

--

--