Production: Player Shield Art

Introduction

In the last post, we updated the enemy from a capsule to sprite art. Today we’re going to add art for our shield powerup!

The Challenge

The challenge is to add a child GameObject to the player with a SpriteRenderer to render our new shield art. Let’s get started.

Add SpriteRenderer

I’ll and add a child GameObject to the Player called Shield and add a SpriteRenderer component with the new shield sprite. player shield inspector

Since we need to activate and deactivate the shield GameObject, I’ll reference it in the Player script where we are handling powerup activation. Then, I’ll assign the shield GameObject in the inspector.

public class Player : MonoBehaviour
{
    ...

    [SerializeField] private GameObject _shieldPrefab;

    ...
}

In a previous post, we added the _shieldsRemaining variable which we can reuse here to know whether or not the shield should be active. I’ll create a new method called HandleShieldDisplay that we’ll call from the Update method.

private void HandleShieldDisplay()
{
    if (_shieldsRemaining > 0 && !_shieldPrefab.activeSelf)
    {
        _shieldPrefab.SetActive(true);
    }
}
private void Update()
{
    RespondToMovement();
    RespondToAttack();
    HandleShieldDisplay();
}

In the ProtectedByShield method, we’ll call SetActive(false) if we no longer have shields remaining.

private bool ProtectedByShield()
{
    if (_shieldsRemaining > 0)
    {
        _shieldsRemaining--;
        return true;
    }
    else
    {
        _shieldPrefab.SetActive(false);
    }

    return false;
}

Here’s the result. player shield demo

Summary

We’re done! We’ll add more polish to the shield later, but for now it’s functional and we can finally visualize the shield when the powerup is activated.

Stay tuned for the next post.
Take care.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s