Day 33 — Improvements: adding limited ammo and a pickup

Hey and welcome!

As you may have seen from the ending gif in the last article we’re going to be adding in the concept of ammo into our game including an ammo pickup to refill it, let’s get started.

First we need a means of visualising our ammo, luckily we’re pretty familiar with that! So let’s go ahead and create a new UI object called AmmoText, position and anchor it to the bottom left of our canvas and then in the UIManager script create a variable of type Text to store it in.

[SerializeField]
private Text _ammoText;
public void UpdateAmmoText(int ammoCount)
{
_ammoText.text = "Ammo: " + ammoCount;
}

Then we simply add in the above function that will update the ammo on screen depending on the int value we pass in. Next let’s add in the following code to our Player script:

private int _ammoCount = 15;private void FireLaser()
{
else if (_collectedTripleShot == false)
{
if (_ammoCount > 0)
{
Instantiate(_laser, transform.position + new Vector3(0, 1f, 0), Quaternion.identity);
_ammoCount -= 1;
_uiManager.UpdateAmmoText(_ammoCount);
}
}
}

The important thing here is that we’re defining a variable called _ammoCount and assigning 15 to it. Then provided that we haven’t picked up the triple triple shot powerup and that our ammo count has a value greater than 0 we take away 1 from our ammo count and call the UpdateAmmoText in our UIManager script to update that. If you’ve been following on we already have a reference to the UIManager in the Player script that we made earlier so we’re just piggybacking off of that.

Ammo Pickup

Next it’s time for our ammo pickup which will function a lot in the same way as our previous powerups so let’s go ahead and create an ammo object to prefab and attach the Powerups script to it making sure that we include our 2D collider and 2D rigidbody as well. Once that’s done add the prefab to the powerups array in the spawn manager object making sure to increase the Random.Range we have so that it can spawn in.

Next we add this method in our Player script:

public void RefillAmmo()
{
_ammoCount = 15;
_uiManager.UpdateAmmoText(_ammoCount);
}

Pretty simple stuff here, now we update our switch statement in our powerups script to include our ammo pickup:

switch(_powerupID)
{
case 0:
_audioManager.PlayAudio();
_player.EngageTripleShot();
break;
case 1:
_audioManager.PlayAudio();
_player.EngageSpeedPowerup();
break;
case 2:
if (_player.collectedShield == false)
{
_audioManager.PlayAudio();
_player.EngageShieldPowerup();
}
break;
case 3:
_audioManager.PlayAudio();
_player.RefillAmmo();
break;
default:
Debug.Log("Something has gone wrong with defining the powerupID");
break;
}

The important bit is that we’ve simply added an extra case that calls our RefillAmmo() method in our Player script.

We’re getting pretty familiar with implementing powerups now aren’t we?

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.