2D Galaxy Shooter: Ammo Count

Anurag Chawla
3 min readJun 1, 2021

--

Visualize on screen the ammo count of the player in the form of current/max

This feature of Ammo Count in the form of current/max—here we have assumed that default max of ammo count player having 15. so when player start the game having Ammo Count :15/15 .

This can be visualized by adding Text from UI under canvas- with few properties set for Text like alignments and setting Text as Ammo Current\Max:

Adding Text from UI — Ammo Current/Max

Programmatically in-order to visualize this, there is a communication between UIManger.cs and Player.cs Scripts.

In Script UIManager.cs - added variables as below and during start of the UIManager.cs script, it will initialize the _ammoCount.text Text field.

private int _totalAmmoCount;
private int _totalBaseCount = 15;

Initialize the AmmoCount text during start() method: UIManager.cs Script
Methods implemented in UIManager.cs Script

Here TotalAmmoCount() method is called in Player.cs script in order to know the player the total ammo he is having.

In Player.cs script :Start() method — Get ref to UI Manager and calls TotalAmmoCount()

As when player shoot laser — deduct 1 ammo from Total and as when player acquire Triple-Shot power- up & fires 3 lasers, so deduct 3 ammo from the rest of the Total ..This happens in FireLaser method()

In Player.cs Script: FireLaser() method called hen Update is called once per frame

When AmmoFire() method is called, it will update the ammoCount based on either laser is called 1 or 3 (for triple-shot) and it will call the updateAmmoCount() as discussed earlier and update the UIManager.cs script.

Finally when game runs..and player fires laser …and see as below..

  • Limit the lasers fired by the player to 15 shots.
  • When the player is out of ammo, provided feedback through on-screen elements flashing Total Ammo:0
Ammo Current/Max -UI Visualizer

--

--

Anurag Chawla
Anurag Chawla

Written by Anurag Chawla

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

No responses yet