Day 32 — Improvments: visualizing shield strength

Connor Fullarton
2 min readApr 16, 2021

--

Hey and welcome!

Ok I enjoyed implementing this one so I’ve got a fun one for you here where we’re going to set it so that our shield can take 3 extra hits now and the color changes per hit to show the sustained damage.

private int _shieldHealth;
private SpriteRenderer _shieldVisualHealth;
public void Damage()
{
if (collectedShield == true)
{
_shieldHealth -= 1;
switch(_shieldHealth)
{
case 0:
Destroy(_shield.gameObject);
collectedShield = false;
break;
case 1:
_shieldVisualHealth.color = Color.red;
break;
case 2:
_shieldVisualHealth.color = Color.yellow;
break;
case 3:
_shieldVisualHealth.color = Color.magenta;
break;
default:
Debug.Log("Something has gone wrong with the Shield Health");
break;
}
}
}
public void EngageShieldPowerup()
{
_shield = Instantiate(_shieldPrefab, transform.position, Quaternion.identity);
collectedShield = true;
_shieldVisualHealth = _shield.GetComponent<SpriteRenderer>();
_shieldHealth = 4;
}

Let’s look at the EngageShieldPowerup() to start with, in here you’ll see that we’re setting our new int variable _shieldHealth to 4 so that we can make our shield take extra hits as we reduce the number by 1 per hit in the Damage() method. Next we’re adding the sprite renderer component of the shield that gets instantiated to our _shieldVisualHealth variable. The sprite renderer component looks like this in the inspector:

With that done we can look at our new switch statement in Damage(), specifically the _shieldVisualHealth.color = Color.magenta; line that gets called when the shield health drops to 3 after taking a hit.

As you can see in the image above it has a Color value which we can access using _shieldVisualHealth.color, with that done we can use a built operation called Color.magenta to set the colour to magenta. Then we proceed to turn the color to yellow and then red before destroying the shield altogether.

Here’s an image of the full list of colour properties available:

Sadly no hot pink or regular pink this time, however if you know the rgba value you can do things this way:

Color newColor = new Color(0.3f, 0.4f, 0.6f, 0.3f);

Then instead of assigning the _shieldVisualHealth.color to Color.red you would assign it to newColor instead.

There’s a little sneak peek of what’s to come for the next article in the bottom left there.

--

--

Connor Fullarton
Connor Fullarton

Written by Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

No responses yet