Creating the Ammo PowerUp

Daniel Jennings
3 min readAug 8, 2021

Since we just limited Ammo give the Player a chance to recover

Task:

We need to create a new PowerUp that refills the ammo of the Player.

Plan:

On collection of the PowerUp, refill ammo count and the UI.

Process:

We will need a Prefab to collect as well as logic updating.

Let’s start with the logic updating first.

PowerUp script

Add AmmoPowerUp to our enum of tags.

Update our Collision logic to Pass along the new tag to Player.

Player script

In our Player script we follow the simple steps: Reset ammo, update UI.

Ammo.Reset()

For the Ammo script I added a const that will hold our AmmoAmmount. This will get rid of the magic number for our ammo count.

I also added a call to Reset in the Start method so we can use the set logic only once.

Reset sets the ammoCount variable back to the AmmoAmmount const value.

Creating our new Prefab

Find the png file you want to turn into a sprite. Make sure the png is in your Assets folder and then drag the png to the game hierarchy to create the sprite.

Use the other PowerUp prefabs as templates to make sure ReloadPowerUp is setup properly. As a reminder, we will need a collider 2D, Rigidbody 2D, and the powerup script with sound clip parameter.

SpawnManager

If we want for our new PowerUp to spawn randomly with the rest we can’t forget to update the SpawnManager.

Let’s add the PowerUp to the list and then check out the inside of the script.

I accidentally hardcoded the max value of the spawn randomizer. Let’s change it to the length of the array so it is future proof for any PowerUps we may want to add.

--

--