Day 37: Limiting the players ammo

Tyler Smallwood
3 min readMay 28, 2021

Objective: Allow the player to fire 15 times before being told out of ammo

To start off, we will need to figure out where we want to apply the ammo count to and if out stop firing. We should check for enough ammo on the firing method in the player script. First thing first is to create variables for the ammo.

Next is to check if we have enough ammo. We’ll do this before we instantiate anything. Check to see if our ammo is greater than 0, if so fire, if not return.

That’s how we limit the player to firing more than 15 times. Next is to connect the UI. In the picutre above you will see the _uiManager.Ammo(_ammo);, We created a method in the UIManager to display the ammo count. We will need to get a reference to the ammotext and the outofammoText.

Next is to update the ammoText when ammo is fired. We pass in the ammo amount from the player script and add it to the string. After we display the amount we check to see if we have less than zero ammo. If so, we need to activate the outOfAmmoText.

In the inspector, we need to assign the variables. We need to create the text first and position them.

For outOfAmmoText, we need to disable it so it wont show in the game when the player has full ammo.

Before we close this off, I’ll explain how I got the flashing red and white text that scales up and down.

Like we did before with animating will create a new animation on the outOfAmmoText game object. When the animation window comes up to add keys, we’ll hit record and about 15–20 seconds in we’ll change the color and scale. We’ll move another 15–20 seconds and scale it back to normal and change the color back. This will allow it to look more natural.

You can time out the frames more to your liking. This is all up to you. We now give the player feedback that they are out of ammo!

--

--

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer