Space Shooter Challenge: Ammo Count

Let’s add the ability to limit the player’s ammo count, making the game a bit more of a challenge as opposed to spamming the spacebar.Firstly, I need to create an int variable for ammo count. For now, we can assign it in the inspector to 15. In the fut…


This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee

Let’s add the ability to limit the player’s ammo count, making the game a bit more of a challenge as opposed to spamming the spacebar.

Firstly, I need to create an int variable for ammo count. For now, we can assign it in the inspector to 15. In the future, we could look at adding a maximum ammo count so the player can’t accumulate too much firepower, but for now 15 seems like a good starting value.

Now in our FireLaser method, we can enclose the whole block of code inside an if statement, checking if the ammo count is greater than 0. If so, the FireLaser method continues, and the ammo count should then be deducted by 1. We can also create an else statement to dictate what happens when the player has no ammo. For now, a simple Debug.Log can show us things are working as they should be.

Let’s add some UI for the user to keep track of their ammo count. To do so, I created an empty game object within the Canvas, I did so, as I want to store both an image and text. For the image, I used three laser sprites. The text can simply say “Ammo:”.

To give the UI some functionality, we can create a Text variable in the UIManager Script. Next, I created a public method called UpdateAmmoUI, and assigned an int variable called _ammoCount to be called with it.

Inside the method, I appended the _ammoCount variable to the Text variable.

The Player Script already has a reference to the UIManager, so we can simply call the new public method after we deduct the ammo count, passing in the variable _ammoCount.

Within the if (_ammoCount > 0) function of the FireLaser method

If the ammo count reaches 0, I want the user’s vision to be drawn to the ammo count so they are aware. To do so, I can use an if statement and within that, Start a Coroutine.

This Coroutine will look very similar to the GameOverFlickerRoutine, enabling and disabling the Ammo Text every 0.5f seconds.

To tidy things up, we can create a WaitForSeconds variable for both Coroutines to use, instead of creating a new WaitForSeconds constantly.

In addition to the Ammo Text flickering, I also decided to have an audio cue for running out of ammo. To create this, I layered a few different clicking sounds. Once again, make a new Event, then replace the Debug.Log code in the Else statement of the FireLaser method with the new Event Posting.


Space Shooter Challenge: Ammo Count was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee


Print Share Comment Cite Upload Translate Updates
APA

Calum Slee | Sciencx (2021-05-27T02:06:17+00:00) Space Shooter Challenge: Ammo Count. Retrieved from https://www.scien.cx/2021/05/27/space-shooter-challenge-ammo-count/

MLA
" » Space Shooter Challenge: Ammo Count." Calum Slee | Sciencx - Thursday May 27, 2021, https://www.scien.cx/2021/05/27/space-shooter-challenge-ammo-count/
HARVARD
Calum Slee | Sciencx Thursday May 27, 2021 » Space Shooter Challenge: Ammo Count., viewed ,<https://www.scien.cx/2021/05/27/space-shooter-challenge-ammo-count/>
VANCOUVER
Calum Slee | Sciencx - » Space Shooter Challenge: Ammo Count. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/27/space-shooter-challenge-ammo-count/
CHICAGO
" » Space Shooter Challenge: Ammo Count." Calum Slee | Sciencx - Accessed . https://www.scien.cx/2021/05/27/space-shooter-challenge-ammo-count/
IEEE
" » Space Shooter Challenge: Ammo Count." Calum Slee | Sciencx [Online]. Available: https://www.scien.cx/2021/05/27/space-shooter-challenge-ammo-count/. [Accessed: ]
rf:citation
» Space Shooter Challenge: Ammo Count | Calum Slee | Sciencx | https://www.scien.cx/2021/05/27/space-shooter-challenge-ammo-count/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.