Time for the Shields! Part 2

Pavel G Rodriguez Garcia
3 min readNov 8, 2022

The final part to get our shield powerup working was to add the shield graphic to activate whenever the shield powerup is obtained by the player.

Originally what I tried to do was to take the Shield sprite that came with the files from https://filebase.gamedevhq.com/ and instantiate it to the transfrom.position of the player, but that just made the shield appear at the location of the player, but when I moved, the shield stayed in the same spot.

Photo by JC Gellidon on Unsplash

That obviously was not working, so instead of trying to move my code around to constantly update the shield position to the player position in the update method, I just made the shield sprite a child of the Player. Problem solved!

Now came the part where with code I would turn on/off the shield. Under the player script I added another [SerializedField].

Next I connected the Shield that was set as the child to the Player via the inspector.

That now gave my Player script a direct reference to the shield, with the direct reference I would now be able to turn on and off the shield. To do this I updated the ShieldsActive method with the following code.

And the Damage method was also update with the following code.

This made it so that every time the shield powerup is obtained the shield will appear around the Player, and once damage is taken the shield will disappear.

And the final piece to finish the shield power up was tp get the shield to animate.

--

--