Creating an ammo refill powerup

Michael Kline
2 min readApr 11, 2021

Last time, I added functionality to my space shooter to limit the number of lasers the player can shoot. They don’t currently have a way to replenish their ammo, so I’m going to add a new powerup to do just that.

To start, I grabbed an image from the internet that works as an ammo pickup. I created a new prefab that is setup the same way as the other powerups and changed the powerup ID to 3 so I can give it unique behavior in my modular PowerUp script. In the switch-statement for powerup functionality, I added a new case for ID=3 to call a method in the Player object called RefillAmmo. This refills the player’s ammo back to the maximum capacity and also updates the ammo counter on the UI.

Player.cs

This works fine, but I want to annoy my player when they try to fire with no ammo left. A sound effect is a great way to let the player know they have no ammo without making them look at the counter. The player already has an audio source, so I’ll create an audio clip variable and attach a sound clip of my choosing. Maybe the annoying Windows error sound? Or the Discord ping sound? Nah, I’ll make it a simple error beep. I made a new check for fire input but added a condition for ammo being equal to 0. If it is, it will play the error sound and no shots will be fired.

Just imagine you can hear the sound clip

--

--