Space Shooter: Limiting the ammo of the player

Give the player a little challenge!

Timo Schmid
May 8, 2021 · 3 min read

What’s a game without a challenge? I can’t name a game here where there was no challenge. Even casual games have challenges. Don’t get me wrong, challenges are something great in games. Let’s add a ammo restriction for the player!

Setting up the Player.cs script

I. Variables
As always, we need variables as basis for the logic. We need one for the maximum allowed ammo and one for displaying the current ammo the player has available.

II. Adapting existing methods
At the very beginning of the game, we should make sure to give the Player the maximum amount of allowed ammo.

Next up, we should make sure the player can only shoot when there is useable ammo left. For this, we simply need to check if the current ammo is greater than zero before we instantiate the laser. If that’s the case, we shoot a laser and substract one from the currently available ammo.

Changes are highlighted in yellow font.

We want to show the current and the maximum ammo as an UI text element. For this to work, we need two new methods, where one will return the currently available amount of ammo and the other to return the maximum allowed ammo.

Setting up the UIManager.cs script

I. Variables
Same procedure here. We need a variable which tells the UIManager what text elements we would like to manipulate.

II. Adapting existing methods
We want to change the text to represent the current available amount of ammo and to state the maximum allowed ammo. Therefore, we need to access the text components of the just created Text variables:

III. Details
Detail work is important! Yes, also in game development. Consider the following things we want to implement:

  • We should assign the text variables inside of the script as well by checking for the GameObject name
  • The player should just see the little instruction text at the beginning and no other UI elements
  • After the player has shot the asteroid, the UI elements should appear on the screen

If you want to, you can also change the text color. Let’s say we want to change the text color to red when the player has five or less ammo left. For this, we have to access the color property of the ammo “Text” GameObject

End result
Here is the end result with the implemented color changing of the ammo text:

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Timo Schmid

Written by

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.