Show Current and Maximum Ammo in UI

Objective: Using Unity add a UI element that shows both the current and maximum laser power levels

Current and max ammo is shown for laser power and missiles

Currently the game has two types of ammo lasers and missiles, so create UI text objects named Ammo_text and Missile_text. Anchor them in the upper right corner, as shown above, with font size of 20 and set color to white. Use SerializeField to assign these objects to the variables below.

Variables for ammo UI in UIManager script.

In void Start() in UIManager script set these variable to their starting values, see below.

Initial values for laser power and missles in UIManager script.

The UIManager has two functions, shown below UpdateAmmo() and UpdateMissiles(), that are called by the Player script when the player fires weapons. The maximum ammo is current 15 for laser power. The UpdateAmmo function has an if statement to detect when TripleShot powerup is active, i.e. ammo = 500. While TripleShot is active the laser power is infinite until the powerup expires.

Update Ammo and Update Missile functions in UIManager script.

The Player script calls the _uiManager.UpdateAmmo() and passes the current _ammo value when it fires a normal laser or activates a Power_Powerup.

Excerpt from FireWeapons() function in Player script

If TripleShot is activated then the following code is executed, allowing triple shot to be fired without costing ammo. The _ammo is set to 500 to denote infinite ammo, but after the powerup expires is set to 15. So triple shot does not cost ammo while active and replenishes the laser power.

In Player script, the TripleShot powerup is activated and then turns off once its duration time is up.

Similarly the Player script calls UpdateMissiles() function in the UIManager when the Missile_Powerup is activated or when missiles are fired. The script below checks to see how many missiles are left and is executed following firing missiles, i.e. the F key is pressed.

Excerpt from void Update Player script.

This article shows how to implement a nice visual for both current and maximum ammo levels for both missiles and laser power. In addition it also shows when the TripleShot powerup is activated by displaying “Laser Power: Infinity”.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store