2D Games Framework: Ammo Collectible

Manuel Cabacungan
3 min readJun 4, 2022

--

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

  • Import the image into the Unity Project pane as we’ve done before as a 2D sprite.
  • 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 “Ammo_Powerup”.
  • Under “Sprite Renderer”, change the Sprite field to the one that you imported for the ammo powerup image.
  • Since this sprite does not have an animation, if the source powerup prefab had an Animator component, disable the Animator component.
  • In the settings for the Powerup script, change the Powerup ID field to 3.
  • In the Hierarchy pane, drag the “Ammo_Powerup” 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 “Ammo_Powerup” in the scene. We used it to create the prefab and now we do not need it in the scene at this moment.
  • In the Hierarchy pane, select the Spawn_Manager game object.
  • In the Inspector pane, under the Powerups array, change the size to 4.
  • From the Project pane, drag the Ammo_Powerup prefab to the Element 3 field in the Inspector pane.
  • Open the Powerup.cs script.
  • So far, we only have 3 powerups: Triple shot, speed, and shields. In the comments for the _powerupID, add that 3 is the ID for the Ammo powerup.
  • In the method OnTriggerEnter2D(), in the switch statement, add a case value for the Ammo powerup. In this situation, add a case value of 3.
    — We will add a method RefillAmmo() to the Player.cs script in the next steps. So, for the case value of 3, add the statement to the method RefillAmmo();
  • 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.
  • When we test the game, the Ammo Powerup will fall at random and crease the ammo count to 15 if the player’s ship catches it.

--

--