Show Current and Maximum Ammo in UI
Objective: Using Unity add a UI element that shows both the current and maximum laser power levels
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.
In void Start() in UIManager script set these variable to their starting values, see below.
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.
The Player script calls the _uiManager.UpdateAmmo() and passes the current _ammo value when it fires a normal laser or activates a Power_Powerup.
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.
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.
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”.