Welcome back, My favorite game developers.
In this video, we are going to become invincible.
So now when we get shot once, the second time,
we are invincible and you
notice that we did a little flash,
which is very cool.
Now we are also going to become
invincible when we do the torpedo.
So nobody can hurt us while we are doing our torpedo.
Obviously we didn't get there,
but let's not worry about that.
Let's first of all got to go and
create our invincibility mechanics.
So let's get started.
O k.
So now whenever we get shot,
if we are, for example,
caught up between the three skeletons right here,
it might be just a little
bit unfair for the player playing
our game that he just dies
immediately and doesn't even realize what happened.
And another thing,
a really nice thing that I would like to
add as a game mechanic to our game
is that whenever we are dashing,
we have our player just a little bit
invincible for a couple of
seconds, maybe even milliseconds.
So for that, we are going to
implement this mechanic of having
our player be invincible
for at least a couple of seconds.
So Let's go into our player health handler.
And in here I'm going to create two variables.
The first one is going to be
a serialized field and it's going to be a float,
and it's going to be the sensibility time.
So how long does the player stay invincible?
It's going to be added as a standard of 1 second.
And then I'm going to create
a private bool and call it the is invincible.
So when the player is invincible,
there are a couple of things that are going to happen
and a couple of things that won't happen.
And one of the things that won't happen is that
the player will not take damage.
So in here, we are going to
check before we start damaging the player.
If the player is invincible,
then we are going to actually
know what I meant to do is bow.
So in here, let me remove this one.
And, and an invitation to those.
What I meant to say if the player is not invincible,
so it's not invisible means we will
add an exclamation mark behind this,
and this will change the entire thing.
So if he isn't invincible,
is invincible is true,
which means the whole condition
is false because of the exclamation mark,
this means we do not access this.
This means whenever we are invincible,
is invincible is true.
We do not get hurt.
So excellent.
Let's continue on.
The next thing we want to do is
we want to make sure that we
then stop being invincible
after a certain amount of time.
So for that we are going to have to create a coating.
So I'm going to issue it to you as a challenge.
So turn the player back using a coating.
You'll need to create a core thing that turns
the player back from his invincibility,
should use the invincibility time variable
and you need to call the coating
in the appropriate place.
So with that information of the video right now,
and go to the challenge.
Okay, welcome back.
How did you get on with that?
You know, what I love about
the challenges is that it keeps you accountable.
So if at this point you still did not
know how a court in works after all the challenges,
you will have to learn at some point because I will
keep asking you to do
something until it is ingrained in you.
So let's take it easy bit and
let's go ahead and create an eye enumerator in here.
So I believe it's pretty easy now.
So it's player is invincible.
Or turn back to what should we call this layer?
Let's just call it is in invincible.
They, they're not invincible.
Okay, Let's call it that.
And here we are going to yield
return new weight for second.
I mean, by this point,
it should be like just sneezing or breathing.
So we yield return new wait
four seconds, the invincibility time.
And after that invincibility time is
invincible will be back to false.
And also I want to make sure that as soon as we
start the is invincible is set to false.
So that we are not
invincible as soon as we start with that done,
I believe that's everything that we need to do.
So I'm going to save this.
I'm going to go back into our game.
And let's run the game.
And let's see what happens.
So when i, okay, so first off,
we need to add maybe a couple of more seconds.
So doom and sensibility time,
let's make it two, and let's run it again.
So this will be the time.
So we get hit once.
There we go. Now we get it twice, we still die.
Why is that?
Let's see what the problem is.
Let's make this public so we can see
clearly if we are actually changing what's happening.
All we haven't started the cold in here.
So I'm going to make sure that we do.
In here, Where should we call it 0?
So also what I want to do is whenever we do get hit,
we also need to change the is invincible to true.
So there are many steps that I forgot to do.
And after we get it,
I'm going to start the co-routine.
And the coroutine will be the layer, not invincible.
So there are a lot of things.
So let me tell you what I forgot to do.
And here, as soon as we get hit,
we want to set the invincible to true.
And once we are invincible,
we are then going to start the coroutine.
And because the is invincible is true,
we no longer get damaged.
So this line was crucial.
And I don't even know how I
could have forgot something so important,
this is the whole essence of
making our player invincible.
So I hope you understand what's happening.
I hope you're not too mad of
the mistakes that I sometimes make.
And let's go ahead and run the game.
So there we go, we get hit once, we get it twice.
There you go.
You can see that the second hip did not kill us.
Why? Because we had the invincibility time on.
So we can see right here it was on.
Let's test this out again because I
feel that we should have maybe taken one more hit.
So there we go is invincible and we died.
I still feel that there's something not
quite wrong and I'm quite right. So 50.
So let's make sure that it's working.
Let's set it to maybe five seconds
and see if that lasts longer.
Because I can see in here that this is
not changing 1234, okay,
so I know I think everything is working,
but for some reason we aren't
seeing this change right here.
But no worries about that.
We know that it's working because when we
increase the size of the time, we are invincible.
We can stand on the face of the sort a bit longer.
Okay, great. Now, that is all fine and dandy.
But here is the problem.
We have no idea that we are
invincible for a certain amount of time.
So what we're going to do is we are going
to try to visualize this.
So do that.
What I think I'll do is
I'm going to access the body right here.
You can see that we have the colors and when we get hit,
I'm going to make the player blink.
So I'll make him blink once, twice, three times,
or keep a blinking until the time that we have is over.
So Let's go ahead and do that.
So I'm going to go in here and I'm going
to get a reference to the sprite of the player.
So serialized field.
And it's going to be the sprite renderer.
And it's going to be the player sprite.
There we go. And in here we are going
to create a numerator.
And it's going to be the flasher.
And in here what we'll do is we are going to,
first of all determine how many
flashes do we want to have.
And then we'll create a for loop.
Wait for it.
You know what I'm talking about?
It's going to have the int equals 0,
while I is less than how many flashes do we want?
Let's say five flashes.
And I plus, plus.
Okay.
So stay with me.
I'm going to leave you properly.
I'm going to first of all add a bit
of spacing in here and then I'm
going to access the player sprite dot color
is going to be equal to a new color.
Open up the brackets or whatever they are called.
And then I'm going to write in here,
we will keep the a the same.
We will keep the,
let me just duplicate this line twice.
We will keep the a.
Then we have the ordinal.
We start off by red,
red, green, and blue.
And finally, we will have
the last one which should not take a comma.
Can see right here we don't have a comma and it's going
to be the, the a.
But in our case, first of all,
we want to reduce the a.
So let's set it to maybe 0.1.
Then after this, we are going to yield return new.
And we are going to wait four seconds.
And this is how fast we are going to be blinking.
So I'm going to set it to your 0.1.
When we don't have a 0 or just a point,
that means we have 0.1.
It's just a bit of a faster way to do things.
And then we are going to go back and copy
this line and put it in here.
And instead of 0.1,
we were going to have the one.
And let me just add a tab
in here for implementation purposes.
So just understand everything.
And then we are again going to
yield weight return for 1. Second.
Save that.
And now we have to start the coroutine somewhere.
When we get hit right
after we set the invincibility to true,
are going to start the co-routine,
which is going to be V flasher with the parentheses also,
these are called parenthesis, I just remembered.
Maybe they'll carpool and parentheses and ways we
save that what is happening in here.
So first of all, we have the loop.
We already know how the loop works. We start off at 0.
We do it four times,
five times actually is 4,
0, 1, 2, 3, and 4.
So 5 times, we increase by one
every single time and we'll loop through.
So what are we doing in here?
We are changing the color.
We keep everything the red, green,
and blue the same except for the alpha.
And in here we are doing the same.
So the first one we are reducing to alpha to 0.1.
And the second one we are making the Alpha 1.
And what are we doing in here?
Well, we already know how yield
return you wait four seconds works.
First of all, we have the alpha at 0.1.
We wait for 0.1 seconds.
Then we turn it back to one.
We wait for 0.1 seconds and then
again start the loop and we keep doing this.
So we'll go, okay,
and we keep doing this and this will
create a flashing effect,
not believe me, that's that out.
So I'm going to go back in here.
I'm going to run the game and let's see what happens.
So I'm going to approach the enemy.
He shoots me and nothing happens because obviously,
every single time we have to make
the same mistake that we need to add the player sprite.
Then we drag the body,
we save all of that.
We going to apply the changes and we're going to run.
And now we should see that we, There we go.
You can see that we start blinking.
When we stop blinking,
that means that we are no longer invincible.
Now the only thing here, the only trick,
tricky thing here is that we want to make
sure that the invincibility time and the flushing
coincide so we don't start
flashing and then for some reason we
stop flashing yet we are still
invincible or the other way around we keep flashing,
but we are no longer invincible so we die.
We're not sure what's happening.
So there will be a bit of
a timing issue that you'll need
to tweak around and make sure it works.
But with that said,
I'm going to issue you an extra challenge.
How cool is that?
I am 100% sure you did not see
that coming because I want to make
the player invincible when he does
the torpedo or the dash or whatever you call it.
So that information, pause the video right now
and make the player also
invincible when he does the torpedo.
Oh, okay, welcome back.
So what are we going to do in here?
Obviously, we have the flasher right here,
but we want to make sure that we
call it from another method,
which is the player controller.
When we are, Where is it,
Where is it switching gun? The player dashing.
So there we go in here,
we want to make sure that we call
this whenever we are dashing,
how are we going to do this?
Well, it's pretty easy.
We need to start a co-routine.
So we are going to look back in here.
Do we have a coroutine?
We are going to go ahead and get
the component, the player health.
Whereas it layer handler.
There we go.
And in here we are going to,
before we get the component, start a coroutine.
And I think because this one is what's the name?
And just check on No,
Actually, sorry for that.
Dot start coroutine.
And here we are going to access the flasher.
Can we know we need to make it a public on enumerator.
So flasher, it's not allowing me to.
Okay, So I think we have to do it like this.
So start coroutine.
And there we go.
Save that.
So as you can see,
even I wasn't sure how this is going to work,
but apparently we need to start the coroutine.
We access the component,
we get the player health handler and NBA player handler.
We get the flasher.
So save that.
Let's test this out.
Back in our game.
We run the game.
And now when we do the dash, there we go.
You can see that we are flashing.
How cool is that an phi dash?
We should see that I don't get,
okay, So that didn't work.
Let's not worry about that.
It's going to be hard maybe to time
the time we are invisible with the dashing,
with having the enemies shooters.
Don't worry about that because
in the next video we are going
to be creating some spikes traps.
And this is a great way to test
out our invincibility when we're dashing.
So if we walk through the sprites will die.
If we walk through the sprites While dashing,
we will not die because we will be invincible.
So that's said.
There is also one more thing that even though we are
flashing in here on the player controller,
we are not invincible.
And make the player invincible was the challenge,
and I just noticed that before we turned everything off.
So to avoid the confusion,
what I'm going to do is back and the where are we,
the player health handler.
I'm going to create a method which
don't be a public void.
Make layer in sensible what was not invisible,
okay, make their invent symbol.
And in here I'm going to first
of all turn the invincibility on.
So is as and invincible will be equal to true.
Then we are going to
start the coroutine of the flash one.
And finally, we are also going to start the coating.
Please, let me start courting of
the make to what was it called?
Player not invincible.
So now we have one method that does everything,
one method to rule them all.
So we can remove this one from here.
We can remove the flasher and the true.
And we can do one thing and that is called
the make player invincible,
save that, and then end the player controller.
We don't need to call the flasher manually.
We can just say Get Component,
Layer health handler, dot, make, player invincible.
There we go.
So let's test this out again.
Back in our game, run the game.
Should us.
We can't shut once.
We got shot twice and we died.
What is the problem?
Did we make it only 1 second?
Let's make it through R3.
Three seconds, run the game.
Again.
We get shot once, twice, three times.
Okay, so this is working.
And then when we do the dash, there we go.
We are dashing.
Okay, we didn't test it out properly.
In the next video, we will make sure of that.
I'm sorry for the length of the video.
Again, this is something good.
I believe we learned how to
start a coroutine from a different script.
And we also learned how to create
a method and compact everything into a single place.
I think it's good,
a good habit to them.
So thank you so much for watching.
I hope you are enjoying,
I hope you're committing your changes.
And I'll see you in the next video where
we will be creating spikes.
We also had an extra challenge which is very good.