Nerd For Tech
Published in

Nerd For Tech

Details - Visualizing the shield strength

More shields? More power, then!

As we just implemented the ability to have multiple shields activated at the same time, it’s now time to add some details to it. We already have the UI elements which represent the current amount of shields. However, the shield visualizer on the player remains static. Let’s change that!

As a reference, that’s what we’re going to implement:

The implementation is done pretty quick! We just need a new method which will handle the opacity of the shield visualizer. For that, let’s take a quick look at how to change the color inside a script.

We need to access the Sprite Renderer component in order to manipulate the Color property. Then, we assign a new color using RGBA values between 0 (nothing) and 1 (full). As we only want to change the opacity, we only need to change the A value and can leave the RGB values at 1.

We can have three shields as a maximum. Instead of using three different if statements, let’s use a switch statement for a cleaner code.
Every shield will add a third to the opacity. Therefore, one shield has 33% opacity, two shields 67% and three will have 100%.

As RGBA values usually have an Integer number, we need to convert the values for 33% and 67% to be of type float. This is done by simply adding ‘(float)’ before the number.

Calling the method at the right places
Last thing to do is to call the method whenever we collect a shield or take damage.

Calling the method inside void Damage()
Calling the method in void ActivateShield()

Having added these few lines of code, we now have a successful implementation of the behavior we wanted. The shield is now dynamically updating according to how many shield we have.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Timo Schmid

Timo Schmid


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 :)