Visualising ammo count in Unity

Daniel Kirwan
3 min readApr 22, 2021

--

After letting the player know how many hits their shields have left before it is gone I needed to tell the player how much ammo they have left to fire. Because currently they can fire to their hearts content and it makes the game very easy.

You could do this by having text on screen like 10/15 which would mean you have 10 shoots left out of a max of 15. I decided to go a different route and using images similar to the shield and health implementation. But his left me with having to create 16 images that would represent each ammo count.

For this I used a paid for tool called Asset Forge, this is a great modelling tool created by Kenney. It is a tool that lets you create 3D models using already existing parts. I used this to create the environment for DangryRun, which is my endless runner game. But what you can also do with this tool is export sprites of your models for 2D games and this is what I did.

I found a bullet model that comes with the tool and proceeded with adding 14 more bullets which made a total of 15. Once I had them all lined up and ready I exported the sprite and the I changed the colour of one bullet wo white and then exported that sprite. I repeated that process until I had 15 white bullets which would show that the player has no more bullets left to fire.

Now comes the same process in Unity for the set up. Like for the shield and health I need to created an array of sprites for the ammo and an Image for the position on the UI.

Then in the Unity editor you drag each sprite into the correct array slot. As arrays start at zero we go from 0 bullets to 15 and when we’re updating the sprite after we’ve fired a laser we will show the correct amount of ammo.

You can see above the array and the sprite that is associated with it.

You can see above the images are being changed every time that the player fires and we’re doing that in the player class whenever they’re firing a laser.

You can see in the if statement above that we’re decreasing the current ammo variable and then updating the UIManager that is responsible for changing the sprites on screen.

And that’s it for todays article. Come back for more when you’re ready.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet