Collecting Ammo System

Renato Figueiredo
2 min readDec 14, 2023

Objective: Learn how to implement a powerup that grants ammo to the player.

Now that we have our Ammo System working, we need to make sure that our player can regain ammo to fire back at our enemies. How can we do that ? Well, we already have a powerup system implemented, how about we just create a new powerup, that grants ammo to the player ?

Creating our Ammo Powerup and animating it.

Now that we have our object, we need to make sure to add a RigidBody2D and a CircleCollider2D. I’m going to copy the exact components from the other Prefabs, and update all the variables needed, such as our Enum for the Powerup Type, and the Audio Clip.

Adding the missing components and updating our variables.

Now we have to make sure that our SpawnManager has the GameObject to spawn at Random.

But how do we apply the Collecting Ammo feature to our Powerup ?
Similar to other powerups, such feature is done in the player, and accessed in our Powerup.

We can have a method that returns ammo to the player, and make sure to call when the player collects our powerup.

public void ReceiveAmmo()
{
_ammoCount += Random.Range(10,16);
_uiManager.UpdateAmmoAmount(_ammoCount);
}

Having this method, will grant the player a Random Amount of ammo, from 10 to 15. Afterwards our UI Manager updates the ammo display on the screen. Now we just need to access this method from our Powerup.

private void ActivatePowerUp(Player player)
{
AudioSource.PlayClipAtPoint(_powerUpClip, transform.position);
switch (_type)
{
case PowerUpType.TripleShot:
player.ActivateTripleShotPowerUp(_duration);
break;
case PowerUpType.Speed:
player.ActivateSpeedPowerUp(_duration);
break;
case PowerUpType.Shield:
player.ActivateShieldPowerUp();
break;
case PowerUpType.Ammo:
player.ReceiveAmmo();
break;
}
}

Now all we have to do is test it out.

We can see our Ammo Powerup working properly.

As we can see in the GIF above, when collecting our powerup, it granted a random amount of ammo (14 on the example), and the ammo was displayed on the screen. Now we can grant our player some ammo back!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response