GameDev : a new Powerup, Shield (Part I)!

Matteo Lo Piccolo
4 min readJun 11, 2021

--

To build up the Shield Powerup, the first section is ALWAYS the same.
We take our Shield Powerup and drag to the Scene

Reset the scale to 0.5 and the name to Shield Powerup.
Add every component we need, Circle Collider 2D, Rigidbody 2D and the script.
Then, set Gravity to 0 and check the Trigger toggle.
Set the Layer to ForeGround, change the ID to 3, and make it a prefab.

Drag and drop Shield Powerup in the scene, highlight it, go to Windows, Animation, Animation and click Create.
Save the animation in the Animation folder with the right name (Shield_Powerup_anim) and save.
Then drag and drop your animation to the Animation window AFTER we click Record. Press again record to stop recording.

IN THE SHIELD POWERUP INSPECTOR, GO TO OVERRIDE AND CLICK APPLY.

Delete the prefab from scene, and drag and drop it into Spawn Manager Inspector inside our Array.

I am not going to show you everything for two reasons: I have done this whole process in previous articles and because the best practice for learning is to simply try to do things.

You don’t have to do everything in this order, just don’t forget anything for it to work properly.

For me everything works fine, I have no error, and every of this 3 Power Up spawn. If I collide with Shield, destroy itself.

Obviously the Shield Powerup don’t do nothing, we need an implementation of it.

In my project I have this

I drag and drop it into the Scene, and make it over the Player

That’s what we want, we collect the Shield and, well, we have a Shield!

Now, we try to understand the logic.
When something hit us, we take Damage.
But IF WE HAVE A SHIELD, we’re not take Damage.

So the Shield is a part of Damage System.
Where we have the Damage method, we need a simple check.
If we have a shield, don’t take damage.

But there is an extra.
We use the keyword RETURN.

Why this?

Normally we use a return keyword when we are inside a method that return us something.

In this case, we need “return” for one simple reason.

Let’s look at Intellisense

We have this strange green underline that say :
“Ehi, this code is unreachable”.

What it means?

When we use return keyword as we did before, we simply say
“If we have a Shield, exit form this method”.

And any line of code AFTER THE RETURN KEYWORD just doesn’t get executed.
In this case : we have a Shield, stop run the code.

All the logic becoming this

I write “no damage”, but I could write “nothing”, because what is happen is exactly this, nothing.
The shield is active and if it take a hit destroy itself.
All the logic run inside the Shield implementation.
We make only a check if we have or not the Shield.

In the second part we implement the Shield mechanic!

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!