Updating our Shield System

Renato Figueiredo
3 min readDec 13, 2023

Objective: Update our Shield System to now include a strength for our shield.

Our player currently has the ability to have a shield to protect himself, but the current implementation only protects the player from one hit. What we want to implement now, is to have a Shield Strength, that decays as the player is hit.

Lets start with the basics. Now that our shield is going to have lives, how are we going to implement such thing ? Let’s create a couple of variables to help us through this.

[SerializeField] private Color[] _shieldColors;
private int _shieldStrength;

With our Shield Strength, we can measure pretty much how many lives the shield can still endure. With our Shield Colors array, we can simply update the color display on our Shield, to make it more visible the amount of damage it already took.

Setting our Shield Colors in the Inspector.

Now lets to go the programming part. How are going to activate our Shield ? Well, we already had a public method that our Powerup calls when we collect it, lets just updated it!

public void ActivateShieldPowerUp()
{
_shieldStrength = 3;
SetShieldState(_shieldStrength);
_shield.SetActive(true);
}

By collecting our powerup, we call this method, that sets the amount of strength to our shield to 3, set the state of the shield to the amount of strength it currently has, and activates it. But what does the ShieldState actually do ? Well, lets see it below:

private void SetShieldState(int strength)
{
if(strength == 0)
{
_shield.SetActive(false);
return;
}
_shield.GetComponent<SpriteRenderer>().color = _shieldColors[strength - 1];
}

Whenever we take damage in our shield, we are going to call this method. The idea here is, we pass to this method the current strength of our shield as a parameter. The method updates the color of our shield. In order to this, we simply get the SpriteRenderer component, which is the one that has the Color for our shield, and update the Color component of the SpriteRenderer, based on the amount of strength it currently has, and if it has no strength, it simply deactivates it and returns, preventing from executing the rest of the code.

Now for the last part of our functionality, we need to make sure our shield can sustain damage from our enemies. Inside our TakeDamage method, we going to make a few changes in order for this to happen.

public void TakeDamage()
{
if (_shieldStrength > 0)
{
_shieldStrength--;
SetShieldState(_shieldStrength);
return;
}

_playerLives--;
ActivateWingsEffect(_playerLives);
_uiManager.UpdateLivesDisplay(_playerLives);

if (_playerLives <= 0)
{
SpawnManager.Instance.StopSpawning();
Destroy(gameObject);
}
}

Our TakeDamage method now, simply checks for the strength of our shield, before applying damage to the player. If the shield can still sustain hits, its strength is decreased, and we call the SetShieldState method, to update the Color accordingly.

Our shield fully functional!

Now our player can sustain more hits with its shield, with our new implementation!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet