2D Galaxy Shooter P1: Shield Strength Challenge Activated!

Gabriel Perez
2 min readApr 30, 2021

--

The Shields now have strength! You can collect shield power-ups, and it will increment up to three.

The challenge was pretty straightforward. I had some trouble, in the beginning, figuring out how to change the opacity of the shields sprite renderer material.

I didn’t go with a HUD on this one. Instead, I wanted to indicate the strength of the shield by the color’s alpha.

So every time the player picks up a shield power-up, we activate the shield and make sure we only increment if the player has a shield strength of less than three.

public void ShieldActive()
{
_isShieldActive = true;
if (_shieldStrength < 3)
_shieldStrength++;
_shieldPrefab.SetActive(true);
}

And when the player is damaged, we need to decrement the shield strength by one, and we make sure that when the shield strength reaches zero, deactivate the shield game object.

if (_isShieldActive)
{
_shieldStrength — ;
if(_shieldStrength == 0)
{
_isShieldActive = false;
_shieldPrefab.SetActive(false);
}
return;
}

We always want to return at the end “if” the shields are activated so that it can skip the logic for damaging the player’s life count.

And here’s a function called in the Update() that controls the shield strength color:

private void ShieldStrengthColor()
{
_tempColor = _shieldRenderer.material.color;
_shieldRenderer.material.color = _tempColor;
if (_shieldStrength == 1)
{
_tempColor.a = 0.3f;
}

if (_shieldStrength == 2)
{
_tempColor.a = 0.75f;
}

if (_shieldStrength == 3)
{
_tempColor.a = 1.1f;
}
_shieldRenderer.material.color = _tempColor;
}

These are the results when testing:

A neat feature to keep the flow of the game going!

The next challenge for tomorrow will be the implementation of a secondary fire power-up!

That is all for today. Thank you for your time!

--

--

Gabriel Perez
Gabriel Perez

Written by Gabriel Perez

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

No responses yet