2D Galaxy Shooter: Ammo Collectable

Anurag Chawla
2 min readJun 2, 2021

PowerUP that refills the ammo count allowing to player to fire again..

In my last article , we have mentioned that player fires laser as they have max limit of 15 shots and when player is out of ammo, there is no way to fire it again. So another feature that refills the ammo count back to 15 shots — ammo collectable.

This can be achieved using Prefab — Ammo_Powerup referenced in Powerup.cs script and set the properties in inspector such as Powerup ID =3 speed, audio clip etc .

Created Prefab: Ammo_Powerup
Script PowerUp.cs - having _powerupID of 3

In Powerup.cs script it will call OnTriggerEnter2D() method when Player collided with the powerup ammo collectable having _powerupID =3 it will communicate with the Player.cs script call the method having player reference player.AmmoPowerUpActive() as below

Script PowerUp : OnTriggerEnter2D() method logic for ammo powerup when player collided

In Player.cs script, where the purpose of AmmoPowerUpActive() to refill the _ammocount variable back to 15 and communicate back to UIManager.cs script, so that it will display on UI Canvas.

Script Player.cs : AmmoPowerUpActive() method

It will call _uiManager.UpdateAmmoCount() method as discussed in last article where it update the ammo back to 15.

Script UIManager.cs : UpdateAmmoCount() method to update the ammo to reflect on UI canvas.
Ammo Collectable:Player collect PowerUp and back to Total Ammo:15

--

--

Anurag Chawla

Emerging Technologies - Power Virtual Agents | AI Chatbots | Multi-Cloud & DevOps | UI Technologies - React , Angular | Gaming Technolgies - Unity, XR- AR/VR