Unity — Ammo system

Objective: Understand how to implement an ammo system

What to implement:

  1. Ammo powerup prefab
  2. Display Ammo on-screen
  3. AudioManager to handle multiple in-game sounds̶
  4. Rigidbody and collider for interaction
  5. Scripting to store, display, and update ammo amount
  6. Double checking Unity

I modified the existing prefabs created for speed and powerup. I came up with this:

Dragging the first ammo sprite into the Hierarchy I can work on making a prefab. Rename it Ammo_Powerup. Set all position coordinates to 0, to ensure there is nothing funny that happens during the spawn.

Click on the object > go to Window > Animation > Animation. Drag all of these in there and press play to see the result.

  1. ̶A̶m̶m̶o̶ ̶p̶o̶w̶e̶r̶u̶p̶ ̶p̶r̶e̶f̶a̶b̶
  2. Display Ammo on-screen
  3. AudioManager to handle multiple in-game sounds̶
  4. Rigidbody and collider for interaction
  5. Scripting to store, display, and update ammo amount
  6. Double checking Unity

Right-click on the Canvas object in Hierarchy then browse to UI > Text. If you create this outside of Canvas it won’t be able to see it because it’s not in the visible area of the screen. Name it Ammo, in the text field put in Ammo: with a space after. Set the Size to 20, Set Horizonal Overflow to Overflow so the ammo doesn’t get cut off. Set the Color to white. After all this, set the Anchor to the top right of the Canvas.

Using the move tool, drag it below the score and leave enough room for about 2 digits. It should look something like this:

  1. ̶A̶m̶m̶o̶ ̶p̶o̶w̶e̶r̶u̶p̶ ̶p̶r̶e̶f̶a̶b̶
  2. ̶D̶i̶s̶p̶l̶a̶y̶ ̶A̶m̶m̶o̶ ̶o̶n̶-̶s̶c̶r̶e̶e̶n̶
  3. AudioManager to handle multiple in-game sounds̶
  4. Rigidbody and collider for interaction
  5. Scripting to store, display, and update ammo amount
  6. Double checking Unity

To better manage in-game audio create an empty object in the Hierarchy and name it AudioManager. Right click AudioManager > Audio > AudioSource. Drag and drop a sound already in the Project section into that field.

  1. ̶A̶m̶m̶o̶ ̶p̶o̶w̶e̶r̶u̶p̶ ̶p̶r̶e̶f̶a̶b̶
  2. ̶D̶i̶s̶p̶l̶a̶y̶ ̶A̶m̶m̶o̶ ̶o̶n̶-̶s̶c̶r̶e̶e̶n̶
  3. ̶A̶u̶d̶i̶o̶M̶a̶n̶a̶g̶e̶r̶ ̶t̶o̶ ̶h̶a̶n̶d̶l̶e̶ ̶m̶u̶l̶t̶i̶p̶l̶e̶ ̶i̶n̶-̶g̶a̶m̶e̶ ̶s̶o̶u̶n̶d̶s̶̶̶
  4. Rigidbody and collider for interaction
  5. Scripting to store, display, and update ammo amount
  6. Double checking Unity

Click on the object, at the bottom is Add Component. Click on that and add the Rigidbody 2D and Circle Collider 2D. The settings should look like this:

  1. ̶A̶m̶m̶o̶ ̶p̶o̶w̶e̶r̶u̶p̶ ̶p̶r̶e̶f̶a̶b̶
  2. ̶D̶i̶s̶p̶l̶a̶y̶ ̶A̶m̶m̶o̶ ̶o̶n̶-̶s̶c̶r̶e̶e̶n̶
  3. ̶A̶u̶d̶i̶o̶M̶a̶n̶a̶g̶e̶r̶ ̶t̶o̶ ̶h̶a̶n̶d̶l̶e̶ ̶m̶u̶l̶t̶i̶p̶l̶e̶ ̶i̶n̶-̶g̶a̶m̶e̶ ̶s̶o̶u̶n̶d̶s̶̶̶
  4. ̶R̶i̶g̶i̶d̶b̶o̶d̶y̶ ̶a̶n̶d̶ ̶c̶o̶l̶l̶i̶d̶e̶r̶ ̶f̶o̶r̶ ̶i̶n̶t̶e̶r̶a̶c̶t̶i̶o̶n̶
  5. Scripting to store, display, and update ammo amount
  6. Double checking Unity

Before deep-diving into scripting, we need to ensure that the spawn manager, the thing that spawns objects on the screen knows about the powerup, will spawn it on the screen.

Drag and drop the PowerUp script onto the Ammo_Powerup object. Change the PowerupID to 3, the spawn manager utilizes an array and selects an element out of that array. Now that the object is finished. Click and drag the object into the prefab folder. The object in Hierarchy can be deleted. Before we forget, click on Spawn_Manager, add another element, drag and drop health in the 4th element, labeled “Element 3”

Starting with the Player class.

  • Create _ammoCount = 15; to track the amount of ammo and limit the player has
  • AudioSource _ammoEmpty accesses Audio references to play audio. Serialized the variable to assign the audio file to the variable within Unity.
  • Updated the FireLaser() method to subtract from the _ammoCount method every time the spacebar is pressed.
  • Using an if statement to say if less than 0, play the empty sound and assign the variable 0, to prevent the ammo going into the negative.
  • Updated the tripleshot powerup to ensure it does not enable even when the ammo is 0, the same with laser fire. Both play the ammo empty noise.
  • Update the ammo count on screen utilizing the _ammoCount variable
  • Created a public void AmmoRefill() method to allow other classes to have access to the private _ammoCount method when properly needed.
  • The powerup will set the _ammoCount to 15 when colliding with the player. The typical powerup sound will play like the others and the screen will update with the new _ammoCount.

Onto the UIManager class:

  • Create a private Text _ammoCount; to track the text. Serialize the field to access the field in Unity.
  • In the Start() method, the moment the game starts, it will always be 15 displayed as the _ammoCount in player has 15.
  • Created a public void UpdateAmmoCount(int ammo) method to receive the _ammoCount amount from the Player class.
  • _ammoCount.text will take the value received in int ammo and update the screen.

Powerup class:

  • Within the OnTriggerEnter2D in the powerup class, adding a 3rd case to handle when the powerup is collected.
  • After colliding with the player, the object will be destroyed like the others.

SpawnManager class:

Last script is updating the spawnmanager so it actually spawns it. The only change we need to make here is the random range to 4 instead of 3 to include the new powerup.

If you weren’t saving the entire time, save now. Biggest practice is save save save. You didn’t do anything unless you can prove it with a nice save.

Last part is checking all empty boxes are filled like they should be.

  1. ̶A̶m̶m̶o̶ ̶p̶o̶w̶e̶r̶u̶p̶ ̶p̶r̶e̶f̶a̶b̶
  2. ̶D̶i̶s̶p̶l̶a̶y̶ ̶A̶m̶m̶o̶ ̶o̶n̶s̶c̶r̶e̶e̶n̶
  3. ̶A̶u̶d̶i̶o̶M̶a̶n̶a̶g̶e̶r̶ ̶t̶o̶ ̶h̶a̶n̶d̶l̶e̶ ̶m̶u̶l̶t̶i̶p̶l̶e̶ ̶i̶n̶-̶g̶a̶m̶e̶ ̶s̶o̶u̶n̶d̶s̶.̶
  4. ̶R̶i̶g̶i̶d̶b̶o̶d̶y̶ ̶a̶n̶d̶ ̶c̶o̶l̶l̶i̶d̶e̶r̶ ̶f̶o̶r̶ ̶i̶n̶t̶e̶r̶a̶c̶t̶i̶o̶n̶
  5. ̶S̶c̶r̶i̶p̶t̶i̶n̶g̶ ̶t̶o̶ ̶s̶t̶o̶r̶e̶,̶ ̶d̶i̶s̶p̶l̶a̶y̶,̶ ̶a̶n̶d̶ ̶u̶p̶d̶a̶t̶e̶ ̶a̶m̶m̶o̶ ̶a̶m̶o̶u̶n̶t̶
  6. Double checking Unity

Player:

  • Attach the AmmoGone object created earlier to this field

Canvas:

  • Attach the Ammo object created earlier as a child item of Canvas.

Dedicated and motivated individual learning programming and sharing my discoveries with you.

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

Recommended from Medium

Browser Automation with Rails

Introduced in The Graph

Hacktoberfest 2021

Getting started with FastAPI and MySQL

CS373 Fall 2020: Akshay Mantri

How I nailed the AWS Solution Architect - Associate exam

Cloud Infrastructure Monitoring Service-Prometheus

Is a Coding Bootcamp Right for You?

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
Benjamin Calvin

Benjamin Calvin

Dedicated and motivated individual learning programming and sharing my discoveries with you.

More from Medium

How to set up a ledge grab system for platformer in Unity

Creating a Cooldown System in Unity!

Camera Plugin for Unity

How Object Behave