Adding an ammo count mechanism

GameDev Dustin
2 min readJan 20, 2022

We’re going to add an ammo condition to our player’s firing mechanism.

We’ll make it so that the player is limited to 15 shots before they are out of ammo, but that powerup abilities such as triple shot do not affect this count.

We’ll provide player feedback on the level of ammo (or more accurately charge in our case, since, well, lasers!) via a UI image of the laser that will get shorter as the ammo level reduces.

Let’s start by creating our AmmoCharge_img UI game object.

We’ll assign the laser sprite to it, reposition to the top right corner under our ship lives indicator.

We also want to rotate it 90 degrees so it appears to be horizontal.

Confusing as it may be, our “Height” field is actually now our width field due to the 90 degree rotation of the image.

When we reduce ammo charge, we’ll reduce our Height.
In our code this will be referred to as “Axis.Vertical”.

Let’s add a couple variables, one to hold our new UI GO for our ammo charge one to track the current ammo count, and one to track the size of our UI ammo indicator.

We’ll update our DoNullChecks() method for our _ammoChargeGO variable.

Alright, we now have our ammo charge count in working order.

But the player having only 15 ammo charges, well, EVER, is going to be a problem.

In the next article we’ll add an Ammo collectible powerup to refill our player ammo!

--

--