Hello there.
In this video, we're going to make invincibility for the player so he cannot be knocked twice in a
row.
How are we going to do that?
Let's go to player.
We're going to create a routine.
What is quarantine is pretty simple to understand.
Basically, it allows you to do some actions with a delay.
How do you create a quarantine?
You just go, let's say here and you make it private, a enumerator, you give it a name and our will
be I'm not sure if I can spell it in win ability.
Let me see if I spelled that correctly.
Okay.
This is the correct word.
Yeah.
Now how do we use it?
Very simple.
If we want to make some delay, we just make yield return new wait four.
And here we can choose what we want and we want wait 4 seconds.
And in our case, final is just choose point 5 seconds.
So what I'm going to do debug dot log in wins started or and I'll choose let's say 5 seconds.
All right then here I'll make delay of the 5 seconds and then I'll do the log in wins over.
Now let's see, how does it work?
How do we run this coverage in?
We need just to do very simple stuff.
We can type, start or routine.
Now we need a name or we can just type in the instability here and it will work.
Let's see.
And now it's in the action.
I'm going to open my console hit play.
We're going to see first message.
Then after 5 seconds, we're going to see another message.
In.
We started for five.
In wins over and we can use it just to control bowl and can be knocked, for example.
Let's go and create this boolean private bool can be knocked.
Oh, sorry.
Then we go in here and instead of using debug we're going to do can be knocked equals false.
Can be knocked.
Equals true.
Now we don't start quarantine from here.
We started in the knockback, so once player was knocked, we do start routine invincibility.
And just above that, we're going to say, if not can be knocked, then return.
Yes.
So it can be knocked as false.
Then we return from this function and none of this will work.
And I guess we need to set.
Can we knock to true by default?
Let's go above and do this.
I forgot to do that.
Sorry can be not true.
Now let's go back to Unity and see if I can knock back myself five times in a row.
I can do it once, but not again until time passes.
But if I would say not use this quarantine, I'll commit it out.
Let's see what's going to happen.
I can knock myself over and over again.
And if there is a couple of traps, we can trigger it twice.
So that's not what we need.
All right, so let's uncomment this.
Now we have invincibility, but it's not really clear if player is invincible.
There is no obvious sign of that.
So I want to do that and I want to do that in the style of very old games, how it was before.
I want to make our sprite blink in the time of invincibility.
Let me find the player.
I'm going to open his color.
And there is an Alpha channel which makes it transparent if we want.
Right?
So what I want to do is basically affects like this.
We're going to change Alpha to half of the alpha and then back to original every 0.1 second.
And it's going to look like this.
Got it.
So we're going to get access to a Sprite renderer.
Then we're going to get access to the color and we're going to change.
It's alpha.
Okay, let's go and do that.
Let's go back to code.
We're going to need component of Sprite, Renderer, Private Sprite.
Right, Renderer.
And we're going to name it as S.r.
Then we're going to go to start and assign this component S.R equals to get component of Sprite renderer.
Then let's find our core routine.
And where is it?
Over here.
We're going to make two temp values.
First will be color, original color, which will be equals to our lot.
Color, color of our sprite renderer.
And then we're going to make another color darken color, which will be equals to new color.
And color has four channels red, green, blue and alpha.
We're going to take red of the original sprite renderer as our color dot are.
Same for G.
Same for blue as our color blue and for the alpha is going to be 0.5 like that.
Now here, we said can be knocked to false.
Of course, then we're going to control the color, as are color equals to darken color.
Then we're going to wait for, let's say, 0.1 F.
Then we're going to make S.r.
I'll make a space here, as are color equals to original color.
Then wait again 4.1 second.
And this How is going to work?
It's our color.
Dark in color.
We need to think how long we want invincibility to be.
So lets say if it will be our one second, we need to combine values to make one second in total.
So as our color.
I can just copy this.
Copy.
Based.
How long it is.
One, two, three, four, five, six, seven, eight.
To more.
Then we set the original color and we make can be knocked through again and I can delete this line of
code.
So now, when instability not only sets can be knocked, but also changes color every point.
One second.
Let's try this.
Let's see how it feels.
Boom.
We're blinking and we know that we are under some effect.
And after a couple of tries, player will understand what is happening here because it's pretty intuitive.
I only don't like how it looks.
It's blinking kind of too fast, so maybe we can make it slower in the end.
Let's go back to code and do this one one, then 1.5, 1.5 and then point 25 point 20.25, then point
three.
And then the original color.
And we can delete these.
So now we have less steps, but they take more time.
Let's go and see this.
Yeah, this looks better.
This looks more intuitive and understandable.
Something can fast in the beginning and then it gets in slower.
And I have a feeling when it's going to be stopped.
If you want, you can add a couple of more steps and just copy this, paste it in and make it longer.
So point three, 5.4 I guess is going to be bit more clear.
Boom.
Yeah, that's.
That feels good.
I'll keep this one.
All right, That's it for this video.
See you in the next one.