Once our enemy game object fire on this player game object, then this player need to take damage.
So we're going to do the same thing as we have done in this anime game object.
Firstly, we need to give some health amount to this player game object and then we can just take damage.
So let's select our player GameObject in the hierarchy.
And let's open up the player script quickly.
And let's go to the top of our class.
And we're going to declare a variable, or we're going to give some help to our player game object.
So let's make a public and that will be int whole numbers.
And we can call this one to something like Maxhealth.
And by default I'm going to give around five unit of health.
And let's close that up with semicolon.
And let's make a takedamage function in order to take damage.
So I'm going to make it somewhere in the bottom.
Somewhere over here you can make it anywhere you want.
So it basically doesn't need to be in order.
And we need to make this function public because we're going to call this function another class or
another C script where enemy is attacking on our player game object or shooting.
So let's make a public for function.
We're going to type void and we have to name the function.
So I'm going to call it something like take damage and take damage and make first bracket and some curly
brackets.
And this takedamage function just going to take an argument.
So we're going to pass int damage.
And later on we can just replace this damage in damage variable.
The number we would like to go with.
So we can go with any number that we would like to go with.
For now let's go to Maxhealth.
So we need to subtract.
So we will type Maxhealth.
And we're going to subtract from this Maxhealth minus equal this damage amount.
And let's close that up with semicolon.
So let's suppose we call this takedamage function for the first time.
So the maxhealth by default.
As you can see we have given five.
So this is going to be over here five.
And let's suppose as a damage parameter you just or you just replace this damage a parameter with something
like one?
So the maxhealth is five, so five minus one is going to be four.
And if we call this takedamage function for the second time.
So the max is now four.
So four minus one is just going to be three.
And if we call this takedamage function for the third time.
So the maxhealth is now three.
So three minus one is going to be two.
And if we call this takedamage function for the fourth time.
So the maxhealth is now two.
So two minus one just going to be one.
And if we call this a takedamage function for the last time or the fifth time.
So the maxhealth is now one.
So one minus one is going to be zero.
So the max health of our player game object will at some point it's gonna hit zero.
And let's suppose the max health is already zero.
And if we call this takedamage function so the max is zero, so zero minus one is just going to be minus
one.
So it will move over to negative value.
So before we go to negative value we need to check if the max health of our player game object is already
zero.
Then we need to stop taking damage.
Or we need to stop taking damage.
Or we basically don't need to take damage anymore.
So before we subtract or, uh, basically take damage, we just want to check if max health.
So we're going to check if this max health is lesser than or equal to zero.
So in that situation.
So let's make some curly brackets.
Uh so in that situation what we're going to do, we're going to type this return.
And we do know that after return, what ever line of code we're going to write it will not be execute
means uh, this our max health of player game object will start at zero.
And once it's zero then we just typing return.
And we do know after return, whatever we're going to do will not be execute means our player Max health
will start at zero.
And let's suppose the max health of player game object is already zero by now.
So in that situation we need to destroy our player game object.
Basically player don't have any health so that's why.
So let's make a Di function in order to do it.
And I'm going to make in the bottom.
And this function doesn't need to be public.
So we can simply type void for function.
And we have to name the function.
So I'm going to call it something like type make first bracket and some curly brackets.
So what we want to do inside this function we just want to destroy our player game object right away.
But before we destroy, let's have a look like printing out a message in the console.
Say something like player type, something like that.
We're gonna print out.
So for printing out, we're gonna type debug dot log.
And we have to pass the string that we want to see underneath our console tab.
So I'm going to pass this dot object dot name.
And top of that I'm going to add a message something like diet.
So we're gonna type this plus icon in order to add a custom message and make this quotation.
And I'm going to say something like diet and then close that up with semicolon.
So this dot game object basically is just referencing itself.
And then we just accessing the name.
So the name just gonna be clear.
So this dot game object, basically this player game object just referencing itself.
And by that we just accessing this name and we can see the name in the inspector is player.
So this over here, this dot game object dot name just gonna be player.
And top of that we just added a custom message style as you can see.
So player diet message will be printed out whenever we're gonna call this die function.
So we're going to call the die function in this update function in top of our update function.
Because we do know that update function runs every single frame.
So every single frame we're going to keep track of how much health the player game object currently
have.
And if the max cell is zero, then we're going to call this die function.
And this die function is just going to print out this message in the console.
So let's do that.
So we're gonna move over to this update function.
And top of this update function I'm going to check if if max health is max health is less than or equal
to zero.
If that is the case, then we're going to make some curly brackets.
Means our player game object just loses all of his health.
Then now we're going to call the type function.
So let's call the die function here and make first bracket.
Then close that up with semicolon.
And what this function is doing.
It's really printing out the message in the console player type, as you can see over here.
And we're going to press down Ctrl S in order to save our script.
And let's move to unity.
So we also need to call the Takedamage function in order to take damage.
Or else this player GameObject not gonna lose any of his health.
So the die function not gonna call at all.
So we also need to call the Takedamage function.
But when we need to call that Takedamage function, once our enemy GameObject shoot on this player game
object, then at that moment we're going to do and we do know we're doing inside our enemy script we're
just printing out.
Let me show you first.
So if I select my console tab and then click on this play button in the top.
In order to play our game.
You will see as soon as the game is gonna start and this anime game objects start shooting on our player
game object.
So let's have a look on this and let's get closer to this anime game object.
And we can see as soon as we get, uh, a message in the console says player takes damage just getting
pop up instead of printing out message.
What this player game object need to do this player game object need to take damage.
So let's do that.
So we're gonna get out of play mode, and let's double click on this message in order to move that line
of code.
And we do know we're doing inside this anime script.
As you can see in the left bar, you can see and we're shooting out our way over here.
So the if hit info is basically true.
If that is the case, then what are we doing?
We just printing out a message.
If info basically true means the ray, uh, the enemy that shoot out the ray and the ray basically colliding
with this player.
So that's why that message is getting pop up.
So instead of printing out message, this player game object need to take damage.
So we need to call the Takedamage function right there.
So let's do that.
So instead of printing out message we're going to remove that.
Firstly we're going to check if firstly we will check if hit info dot game object.
Sorry we're going to use this transform.
Then dot get component get component.
And the component we're looking for is a player ship.
So we're going to type this player and then make first brackets.
And then and we will check if it's not equal to null and make some curly brackets.
So what we basically have done, we do know if hit info is true basically means the ray really collided
with this player game object.
It means this player game object needs to take damage.
But before we take damage, what we're doing, we're checking in this if condition.
We're just going over to hit info dot transform and hit it.
Hit info dot transform basically means the, uh, the, uh, the ray collided with the transform.
Well, let's suppose the ray that this anime game object stood out and that ray just collided with this,
uh, player game object.
So hit info dot transform basically means the ray collided with the game object transform.
So let, uh, the ray just gonna collide with this player game object.
So what we're doing, we're just going over to.
We're just taking this hit info parameter in order to extract the game object, the ray hit width.
So the ray definitely gonna hit with this player game object.
So what we're doing we're just going over to this player game object transform component, as you can
see in the inspector.
And by this transform component we just getting this player skip as you can see.
So we just taking the player skip.
And we're checking if the player is skip is not equal to null.
Basically means the player is skip is really attached to this player game object.
And we definitely know that this player skip we can see in the inspector is attached to this player
game object means the player game object is attached to our player game object.
Firstly, we're checking if the ray really collides with the GameObject.
That GameObject has a script called player.
So the ray definitely gonna collide with this player game object.
So basically we're just going over to this GameObject or this player game object Transform.
And by this transform component we just getting the player script.
And we're checking that if this player script is not equal to null, basically means this player script
attached to this player game object.
As you can see, indeed this player game a player script is attached to our player game object.
So now what we have to do, we need to call the Takedamage function from this player script.
And we do know we made the Takedamage function inside this player script.
So we're going to call the Takedamage function right over here because the script attached to our player
GameObject and the really colliding the with this player GameObject and hit info dot transform basically
means the ray.
The.
Basically it's basically means the transform From the ray collided with.
So the ray is going to collide with our player game object.
So hit info dot transform basically means player transform.
So basically we just moving over to this player game object Transform.
And by this transform we just are checking this player skip.
And we're checking if the player is skip is not equal to null.
Basically means this player script is attached to this player game object.
And we can indeed see the player skip is attached to this player game object.
So in this situation what it's need to do, our player game object need to take that damage.
So what we're going to do we're going to use this hit info parameter one more time to extract info.
And we will go to hit Info Transform and Transform.
And then we're going to get the component that we're looking for.
So get component.
And the component we're looking for is player script.
So we're going to pass player.
And you need to make this first packet.
And now we're going to call the function over here that we made inside our player script.
So the function we made is take damage function.
So let's call the function here.
So we're going to type dot take damage and make first bracket.
And then close that up with semicolon.
So uh so what we need to do now we need to pass the argument right over here.
We do.
No we just pass damage parameter in damage.
And now we can choose the number that we would like to go with.
So let's suppose you just pass right over here instead of one you just pass five.
So the player game object just gonna lose all of his health right away.
Because we have given to this player game object five unit of health.
As you can see in the inspector underneath our player Skip.
But I just want to, uh, subtract uh, every single time.
One unit.
So that's why as a damage parameter uh, over here I'm going to pass one.
So, uh, enemy need to, uh, shoot on our player game object five times in order to destroy our player
game object, or we just want to destroy our player game object.
So it's enemy.
Need to take.
Enemy need to fire on our player GameObject five times, and if you just pass over here five instead
of one, then enemy just gonna enemy GameObject just gonna destroy our player GameObject at one shooting,
but we don't want that.
So that's why as in damage parameter, I just pass one.
So let me explain what we basically have done.
We just going over to a player GameObject transform as you can see.
So hit info dot transform basically means the player transform or the collided with the transform.
So the ray definitely gonna collide our player GameObject.
So hit info transform basically means right over here player transform.
And then we just getting the player script as you can see.
So basically we're just going over to this player GameObject transform.
And then we just moving over to this player scape.
And we do know inside this player scape we just made the Takedamage function.
So we're calling the function over here as you can see.
So firstly we're just moving over to this player script.
And then we just calling this Takedamage damage function and we do not take damage function is really
doing is just subtracting from our max health.
With this amount of damage as a damage parameter, we just pass.
One means our enemy game object need to shoot on our player game object five times in order to destroy
our player game object, or in order to lose all of our health player game object.
Basically, that is the thing.
And now we're going to press down control S in order to save our script.
And now let's move over to unity.
So we will see this enemy game object need to shoot on this player game object five times in order to
destroy our player game object.
So let's have a look on this by now.
So just wait for to compile.
And if we just click on this play button in the top and uh, take a look on this, you will see.
So this enemy game object need to shoot on our player game object five times in order to destroy our
player game object.
So let's have a look on this now.
So enemy just gonna shoot for first time.
As you can see, as soon as shooting and the max health, we can see it just go gradually going down
and the max hell is now zero.
And the health now just get stuck at zero, as you can see, because we are checking if the max health
is already zero, then we're not doing at all, uh, at all, anything like that.
So that is the reason.
I hope you get the idea.
And we need to get out of play mode.
So now what we have to do, we need to destroy this player game object right away.
But before we do, we need to make a script in order to check if the game is active or not.
But we're gonna do in the next lecture.
I hope you get the idea that how you can do it, uh, we basically have done the same thing as we have
done in the inside this anime game object.
We just made the Takedamage function and also the die function in order to damage our player as well
as we have the, uh, we have done the same thing in this player game object as well.