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

The implementation for our Shield start by create a bool to check if we have or not a Shield

We create now a method for Shield. We can do in 2 way, with Coroutine or without. I prefer without, but this is a personal preference.
The point is if you want the Shield last for some second and then disappear, or last forever and simply Destory itself when something hit it.
I choose the second solution, so in my case I don’t use the Coroutine.

Now that we have a bool, we need to switch to true when Shield Powerup is collected and instantiate the shield.
So, in addition to the bool, we need the reference to the gameObject Shield.

We create a method to set the bool to true

Now, inside Player Damage method, we check :
if the bool is true, don’t do nothing right? We have the Shield active.

And before return, we change the bool to false.
Because if the Shield is active, and we take hit, shieldPowerupIsActive has to become false.

And return

Now, before test it, we need to call this method to change true to false or vice versa. And where we do it?
Exactly, inside SWITCH STATEMENT.

If we made a simple test an check our lives

The mechanics work.
We take Shield, the bool becomes true, then we take a hit, nothing happens to our lives but the bool is now false, and if we get hit now we take damage again.

Now we have to set the VISUAL PART.
First, we take the reference to the gameObject Shield

I have this in my project

Now, think wisely: when we collect the Shield, we need to instantiate it and …
The Shield has to FOLLOW us right?
It is always above us when we move.

The easiest way to do this is make the SHIELD BECOMING PLAYER’S CHILD.

Shield is a CHILD of Player

Now we have the Shield ALWAYS IN THE SCENE.
It’s a problem?
No.

Because there is method, called SetActive (), to set the object to true or false, so active, or inactive.
Here we have the documentation reference
https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

Now, where we have to set active?
Yes, inside ShieldIsActive()

And where we have to set inactive?
Yes again, in Damage() when we have Shield active

If we test it

The Shield works exactly how we want.

Now we create the animation, as the other Powerups.

My final result is this

My advice here is to try making your own animation.
It’s good practice to try and get things done, even if they don’t work!

We dedicate next lesson to set up the UI System!

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!