Limiting the Max: Ammo capacity (Game Dev day 40)

Jack Leavey
2 min readSep 27, 2021

Objective: Limit the player’s ammo maximum capacity. Make the UI reflect this maximum.

This is an extremely simple implementation with multiple ways to achieve the goal.

I started by making a new variable in my Player.cs called _maxAmmo. I set this variable to equal 99. This will be the absolute cap of ammunition the player is capable of holding at any time.

Now all we have to do is implement a few lines of code into the GetAmmo function. I pseudocode, we are simply saying that if the _ammo variable ever gets above 99, then it instead must be set to be equal to _maxAmmo (which has a hard value of 99.

So now our ammo amount won’t go over, but we can’t see the maximum amount on the UI. There are several ways to go about this, including simply creating another text object and placing it next to your Ammo UI element (see below)

In this example, the “/ 99” is its own UI text object with no script functionality. If you ever wanted to change the ammo cap, you would have to edit this new UI element as well to reflect it.

Two separate objects for the UI.

Another method could be using a bar (see my thruster UI implementation article) and placing the text UI elements inside of it, or using a system similar to the lives display by using sprites. Whichever way is chosen, make sure the player knows how many more times they can fire!

--

--

Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!