2D Space Shooter Game: Ammo Powerup

Manny Cabacungan
3 min readJul 6, 2023

--

Objective: Create a powerup that refills the ammo count allowing the player to fire again

The player’s ammunition is now limited to 15. To replenish the player’s ammunition, we will create an ammo powerup to refill the ammo count back up to 15.

I’ve created an ammo powerup icon of my own in the same style as the others, including a color cycle animation for the rasterized “AMMO” text in the image.

Create your new powerup 2D assets by whatever method works for you. Import them into Unity and remember to set the Texture Type to “Sprite (2D and UI)” and check “Alpha Is Transparency”.

Turn one of the previous powerup prefabs into the new Ammo Powerup prefab.

  • In the Project pane, drag any one of the powerup prefabs to the Hierarchy so that you can turn a copy of it into the Ammo powerup.
  • Select the prefab in the Hierarchy.
  • In the Inspector, rename it to “Powerup_Ammo”.
  • Under “Sprite Renderer”, change the Sprite field to the one that you imported for the ammo powerup image.
  • I made a series of new PNG images for the color cycle animation. To make a new animation, go to the Inspector panel, under the Animator component, and change the Controller to None.
  • In the Animation panel, create a new animation controller for the Powerup Ammo prefab as you did for the other powerups.
  • Add the Ammo PNG images as frames in the Animation panel.

Make the Powerup_Ammo game object into a new prefab:

  • In the Hierarchy pane, drag the “Powerup_Ammo” that you renamed, to the folder, Prefabs/Powerups in the Project pane.
  • The “Create Prefab” dialog box will pop up. Select the button
    [ Original Prefab ].
  • In the Hierarchy pane, delete the “Powerup_Ammo” in the scene. We used it to create the prefab and now we do not need it in the scene any more.

Integrate the new Ammo Powerup into the spawn management, Powerup script and Player script:

  • In the Hierarchy pane, select the Spawn_Manager game object.
  • In the Inspector pane, under the Powerups array, increase the size to 4 so that we can add the Ammo powerup to the list of powerups to randomly spawn.
  • From the Project pane, drag the Powerup_Ammo prefab to the last element field in the Inspector pane.
  • Open the Powerup.cs script.
  • So far, we only have 3 powerups: Triple shot, speed, and shields. Previously, I decided to implement the powerup IDs as an enum data type. Add “Ammo” as the fourth powerup ID:
    public enum _powerupIDs
{
TripleShot,
SpeedBoost,
Shields,
Ammo
}
  • In the method OnTriggerEnter2D(), in the switch statement, add a case value for the Ammo powerup. In this situation, add a case value of “_powerupIDs.Ammo”.

    We will add a method AmmoRefill() to the Player.cs script in the next steps. So, for the case value of “_powerupIDs.Ammo”, add the statement to the method player.AmmoRefill(); and appropriate debug text.
switch (_powerupID)
{
case _powerupIDs.TripleShot:
player.TripleShotActive(_powerupDuration);
Debug.Log("Powerup::OnTriggerEnter2D:switch _powerupID=0 TripleShot");
break;
case _powerupIDs.SpeedBoost:
player.SpeedBoostActive(_powerupDuration);
Debug.Log("Powerup::OnTriggerEnter2D:switch _powerupID=1 SpeedBoost");
break;
case _powerupIDs.Shields:
player.ShieldsActive();
Debug.Log("Powerup::OnTriggerEnter2D:switch _powerupID=2 Shields");
break;
case _powerupIDs.Ammo:
player.AmmoRefill();
Debug.Log("Powerup::OnTriggerEnter2D:switch _powerupID=3 Ammo");
break;
default:
Debug.Log("Powerup::OnTriggerEnter2D:switch No_powerupID");
break;
}
  • In the Ammo Powerup prefab Inspector settings for the Powerup script component, change the Powerup ID field to “Ammo”.
  • Open the Player.cs script
  • Add a new method for refilling the ammo that makes the variable _ammoCount equal to 15 and then updates the UI.
    public void AmmoRefill()
{
_ammoCount = 15;
_UIManager.UpdateAmmo(_ammoCount);
}

When we test the game, the Ammo Powerup will fall at random and increase the ammo count to 15 if the player’s ship catches it.

--

--