Shields Up! Red Alert!
Enabling and Displaying Shield Strength
In todays article we continue implementing the requirements for the first phase of the 2D games certification in the GameDevHQ Professional Unity Developer Program! Todays requirement is to give the player some more power to their shields by increasing the number of hits they can take and giving the players some visual indicators of how many hits they have left. The requirement reads as follows:
Tasks: — Visualize the strength of the shield. This can be done through UI on screen or color changing of the shield. — Allow for 3 hits on the shield to accommodate visualization
For this requirement I’ve chosen to display both an on screen indicator in the UI For Shield Strength as well as the color changing of the shield visualizer for maximum visibility on what could become a very display set of game mechanics and information.
I like to start with UI, so first I’ll create a new Empty Game object in the hierarchy to contain the shield strength image indicator. I then create that element as a child object, and position and align it under the player lives display.
I’ve created some sprites to display the various states of shield strength, so I will pull those into the project and set the image UI element to one of those images.
Next, in my UIManager class I need to add a serializable variable to hold the instance of the image. I also add an array to hold the possible images for each state of shield strength.
As a final update to the UI manager I add a method that sets the sprite displayed based on the shield strength that is passed in and shows or hides the entire shield UI set of elements based on if the shields have a value greater than 0 or not.
In the GameManager class I add a method to call the UI managers UpdateShieldStrength method.
This GameManager class was added in the updates and code cleanup that occurred before starting on the final requirements that comprise Phase 1 of the certification and it’s purpose is to centralize updates to other managers and communications between some components. This allows me to reduce the number of managers that i need to include in other classes like the Player class.
Next, on the Player object in Unity, I increase the maximum shield strength variable that I had the foresight to setup as adjustable from 1 to 3.
Then, in the Player script, I add a method to change to color of the sprite renderer for the shields visualizer based on the shield strength value.
Finally, I add the calls to the UpdateShieldStrength method in the GameManager class and calls to the new color changing the method both where we handle damage and where the power up for shields is collected. and add calls to this new method in the same places as where I added the calls to the GameManager method for updating the shield strength display.
After all that it’s time to play test and check it all in to source control.
Next time, I’m going to limit the players number of shots from their main weapon and implement some audio feed back when they are out of ammo! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.