Adding a shield counter to the space shooter UI
The shield in my space shooter game only blocks one hit currently. I want to increase that to 3 hits, and I’ll need a way for the player to know how much health the shield has left. I’ll use a UI element for this.
I picked out a shield sprite I liked and created an empty object on my canvas and gave it three children Image objects with the shield sprite applied to all them. I made an array of Images in my UIManager script and put in some logic to manage them on the screen, taking some inspiration from the DrawLives method.
This DrawShields method will be called from the player script and is passed an integer for the strength of the shield and then it enables/disables the UI images accordingly. I’m sure there is a much more efficient way of doing this, but it works for my purposes here. And it’s another excuse to use a switch-statement.
On the player side, I need to keep track of the number of shield hits left. I created an integer variable for that and give it a value of 3 when the player picks up the shield power-up then call the UImanager’s UpdateShields method to draw them on screen.
I modified the Damage method a bit to take away shield strength when the player is hit while the shield is active. It removes one from the shield count and updates the shields on the UI with the new shield strength.