Player Ammo and Ammo Collectible: GDJ 18

Brett Blandford
2 min readJun 2, 2021

Player Ammo

The first thing I thought of when it came to ammo was a UI display to let the player know how many shots they have. So I started with creating a text object on my canvas that would then be accessed in my UIManager script. Here I created a public function named UpdateAmmo(int ammo) which updates the UI Ammo display to “Ammo: int ammo” as seen below.

The if check is for the triple shot, where 3 lasers are used. For instance, if the player as an ammo count of 1 then uses a triple shot, the ammo count would be -2 which doesn't make sense. You could also do an ammo check in the player script if you don’t want to be able to use a triple shot if the ammo count is less the 3, but I chose not to do this because of design preference.

Now that I mentioned the player script, lets show where UpdateAmmo is called inside it.

It is called in the ShootLaser() method after the variable _ammo is subtracted from. Any time the _ammo variable is changed, the UpdateAmmo() method should be called. For instance, a ammo collectible that adds to the ammo count.

Ammo Collectible

Since the player has limited ammo, their needs to be a way to give them ammo so they can play for more than 15 shots. I created an ammo power up that adds 3 to the ammo count when collected an implemented it to spawn with the other powerups. UpdateAmmo() when the new ammo is added. This is what it looks like all together!

End of GDJ 19!

--

--