Implementing Shield Powerup in Unity

Rusben Guzman
2 min readJun 11, 2021

--

Prevent player to receive damage with shield

Objective: Create a shield powerup to make the player don’t lose a life when is damaged by an enemy.

In the game the player loses one life when colliding with an enemy. In This challenge I’m gonna create a powerup to activate a shield and protect the player by 1 hit.

First I dropped the shield sprite in the hierarchy as a child of the Player, created a Shield Script and assigned it to the shield.

Then I added a capsule collider 2d Component with isTrigger field checked, adjusted the size according to the sprite and disable the game object.

Now in the player script I declared a variable to store the shield GameObject and created a public function to activate the shield.

In the Powerup Script I call the function to activate the shields when the powerupID is equal to 2.

Finally in the Shield Script I used the OnTriggerEnter2D function to detect when the shield collides with an enemy and simply disabled the shield.

And that’s it. We have a shield to protect our player. The reason why the player is not damaged is because the enemy collides with the shield and not with the player directly so the damage function is never called.

--

--

Rusben Guzman
Rusben Guzman

Written by Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

No responses yet