Last article we created the Shield Powerup Collectible and gave our Player some logic for when they collect the powerup.
But it would be much better if our Player could SEE when the powerup is active. In this article, we’ll give our Player a colourful shield, and activate/deactivate it when necessary!
The Shield Game Object
We have a Shield Sprite, so the first thing we’ll want to do is connect our Shield to our Player and position it accordingly.
We’ll make out Shield a child of the Player, so that the shield will follow the Player’s movement:
Now within our Player script we’ll create a reference to our Shield GameObject _playerShieldVFX.
And then link our childed Shield Game Object inside the Player’s Inspector:
Activating/Deactivating the Shield
To activate or deactivate our Shield, we’ll go into our Player script again, and modify our Activate/Deactivate functions.
Since we have a reference to the Shield’s GameObject, we can simple call _playerShieldVFX.SetActive(bool) when we need to enable/disable the shield:
And the code in action:
Our shield works, and that’s great. But it’s a little stale. Let’s spruce it up with a simple animation!
With our Shield Sprite, we add additional sprites to be used for animation. But after setting the animation up, we run into a little VFX bug:
This is an easy fix — we just have to change the Order in Layer for our Player to be on top of the Shield at all times. We do this by incrementing the Order In Layer to 1:
And that should resolve our issue!
Now our Player knows when they have a shield and when it gets destroyed! This is a very helpful visual cue for our Player when they are navigating through our game!