One way to add extra tension to our game is to limit the amount of ammunition the player has. Here’s one way to implement the feature:
We’ll start by tracking and limiting the ammo our player has.
In our Player.cs script, we add a serialized int for the starting ammo and a private int to track the current ammo. In the FireWeapon() method, after the laser(s) has fired, we decrement the ammo by one.
In our UIManager.cs script, we add a new Text element which we will assign in the inspector.
We create a new UpdateAmmoText() method that takes an int for the new ammo amount. This will update our text element which we will create next.
We add another Text UI element to our canvas, called AmmoText, position and change the settings as we have before with the player score.
Finally we need to update the UIManager from the Player script:
We’ll also check if the player’s ammo is less than or equal to zero. If so, we’ll return out of the method which will also prevent the ammo being decremented to less than zero.
Now our player starts with limited ammo and this is depleted as they fire. When they reach zero ammo, they can no longer fire.
So we have created an issue where the player can no longer defeat enemies! In Part 2, we’ll implement an Ammo pickup to give our player a sporting chance.