Replenishing Ammo with a Powerup

Blake Zoeckler
3 min readMay 28, 2021

--

Last time we gave the player limited ammo. That means when the player has no ammo remaining, they can no longer fire the laser.

Of course, now there’s a new problem. Once the player is out of ammo, they need a way to refill it, or else they will have no way to destroy enemies and the game will become nearly impossible. Today we’ll fix that by creating an ammo powerup.

I made this new animated ammo powerup by taking the same powerup sprite used for the others, loading it into GIMP, adjusting the color, and changing the text to “AMMO”. It’s always a great idea to re-use assets this way, as it tends to be pretty quick and avoids having to buy new assets or spend a lot of time creating them from scratch. We can always spend that time or money later when we want to polish the game!

Back when we created the modular Powerup script, I mentioned it would really easy to implement new powerups. All we have to do now is add Ammo as a new powerup type…

…and add a new case to the switch statement within GrantPowerup. This will call the GainAmmo method we created for the player last time, which resets the player’s ammo back to full. We don’t have to tell the UI Manager to do anything, because the player already takes care of that.

We also have to make sure everything is assigned correctly in the inspector. The new Ammo_Powerup prefab should be set to “Ammo” type…

… and the Spawn Manager’s list of powerups should be expanded, so that it knows how to create that new prefab.

The SpawnManager script can be changed so that it will randomly spawn powerups from any types it has available. Because there are now four powerups to pick from, this Random.Range line will pick a number from 0–3, which is exactly the element numbers we have in the powerups array above. If we were to add yet another type of powerup, this will still randomly choose from all the types available.

Now that we have an ammo powerup available, the player will have the ability to regain ammo and be able to shoot down the enemies once again.

--

--

Blake Zoeckler
Blake Zoeckler

Written by Blake Zoeckler

I’m a passionate and talented software engineer seeking an opportunity in game development.

No responses yet