Day 37: Limiting the players ammo

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!

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Advice for Aspiring Developers

Clone of Apple TV+

Flutter Website hosting on Firebase. Here’s how

How I benefited from contributing to Open Source

Did you try something new this semester?

Here is my referral code for Star Network.

Create and run Flutter app

How to Setup AWS MySql database using free Tier

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
Tyler Smallwood

Tyler Smallwood

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

More from Medium

Use The WASD, Luke!

I think you dropped something — Creating a simple loot drop system in Unity2D

Giving the enemies some teeth

Platformer: Elevator