Unity Guide

Implementing space shooter game features - Shield Strength

A quick review of new features added to a space shooter game in Unity

Fernando Alcantara Santana
Nerd For Tech

--

Objective: Implement a strength visualizer for shields in a space shooter game with Unity.

In the previous post I implemented a boost behavior with a HUD for my space shooter game with Unity. Now it’s time to implement a way to display the strength of the player’s shield.

Shield strength visualizer

In order to create a strength visualizer for the shield let’s add a new C# script to handle the shield prefab:

Now let’s open the script and create these variables:

  • Resistance:

This variable will store the number of hits that a shield can receive until it disappears or breaks. It’s customizable in the Unity Editor.

  • Max resistance:

This variable will store the maximum number of hits that the shield will resist. It will provide a way to restore the shield properties when it’s restored or broken.

  • Shield renderer:

This variable will store a reference to the sprite renderer of the shield so that we can display its strength by changing the color of the sprite.

  • 3 colors:

This 3 color variables will store and define the 3 stages of the shield strength (normal shield, first damage and second).

Then, in the Start method, let’s initialize the variables from above:

We are using Color32 for the colors to be able to use byte values as parameters.

Now, let’s create a new method that will handle the color changes according to the value of the shield resistance:

If the resistance is 0 or greater than 2, the shield is set to its original color.

If you want to know more about the sprite renderer color, you can visit the Unity docs:

Now, let’s create a new public bool method to:

  • Handle the damage received
  • Set the new color by calling the previous method
  • Return a bool value that indicates if the shield is still active or broken
This way, we can call the damage method from the Player script and know if the shield stills active.

Finally, let’s create a new public method to restore the shield in case that it’s still active:

Restoring the maximum resistance and the color indicates that the shield is fully recovered.

Now, let’s open the Player script and create a new variable to store the reference to the Shield script:

Then, in the Start method, we can get the Shield script from the shield prefab reference stored in the _shield variable:

Now, in the method that handles the player’s damage, let’s call the DamageShield method from the Shield script reference and store its value to indicate if the shield should be enabled after each hit:

Finally, in the method that handles the power-ups, let’s add a condition that indicates what to do if the shield is already enabled:

If we run the game in Unity, we’ll see that the shield resists 3 hits and displays a different color after each one:

And that’s it, we implemented a strength visualizer for the shield! :D. I’ll see you in the next post, where I’ll be showing more features added to my space shooter game in Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana
Nerd For Tech

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).