Let's keep some health amount to our player game object.
So let's select the player game object in the hierarchy.
And let's open up the player script by double clicking on it in the inspector.
So what we will do.
We just going to give some health amount to our player game object.
And we also need to make the Takedamage function in order to take damage from this enemy to or from
this enemy to as well as enemy one.
So just wait a moment and we will head over to our code as you can notice.
So let's go to top of our class and let's do it over here somewhere over here.
Let's do that.
So what we will do we firstly let's give it a title.
So we're going to make square brackets and then header.
So let's make header.
And let's say something like hell let's call it health health.
And let's give some health amount to our uh our uh player game object.
And this parameter does not need to be public, so we can make it private instead.
So we can remove this health title as well.
But let's keep that because we're going to use later on with UI.
So that's why we're going to leave that.
So let's make it private.
A private int which can be basically 0123.
And we will do the same thing as we have done with anime two script as well as anime one script in that
health and damage scripting.
So we're going to approach the same way.
So let's give some health amount.
So we're going to call it something like Max health.
And let's set equal to something like five.
And let's close that up with semicolon.
And now what we will do.
We're going to make the takedamage function in order to take the damage.
And let's make it, uh, let's make it somewhere over here.
And we're going to make this function public, since we're going to call this function from another
class or another script.
So that's why once our enemy two GameObject attack on our player GameObject at that moment, we're gonna
call that.
So that's why.
So let's make this function public.
For function we're going to type void and we have to name the function.
So I'm going to call it take damage.
Let's call it take damage.
And it's going to take an argument which will be int.
And we can call this one something like damage amount.
And later on we're going to replace this damage amount with some real numbers.
And let's make this curly brackets.
So this will be int in type basically 01234.
Or you can simply say whole numbers.
So what we do what we're going to do right over here.
We want to subtract our health amount.
So before we subtract our health amount we need to check a check.
If the max health of this player game object is already zero or not.
If it is, then we need to stop damaging our player game object.
So before we damage we just want to check.
So if let's make first bucket and some curly brackets and what we want to check over here, we basically
want to check if our back cell, if it's less than or equal to zero means our player game object don't
have any health amount, it's totally zero.
So in that moment what we need to do we need to stop damaging our player.
So that's why we're going to type return.
And we do know that after return, whatever we're gonna do it's not going to be execute at all.
But let's suppose uh player game object has uh have some health amount.
So else if our player game object has uh, have some health amount.
So in that moment, what we need to do, we need to subtract them from our max cell, this damage amount.
So let's subtract from this max cell.
So max health minus equal damage amount.
And then close that up with semicolon.
column.
So let's suppose we call this take demand function for the first time and max health.
If we go to the top of our class, we have given five, as you can see right over here.
So this Maxwell right over here, it's gonna five in the start of the game.
And let's suppose we call this Takedamage function for the first time.
So this max health is five.
And let's assume this damage amount we pass one.
So five minus one gonna be four.
And if we call this second time this takedamage function.
So the max is now four.
So four minus one it's gonna be three.
And if we call this take damage function for the third time.
So the max health is now three.
So three minus one.
Just gonna be two as.
And if we call this a fourth time, then this max health is two.
So two minus one gonna be one.
And if we call this takedamage function for the fifth time or five fifth time, then this max health
is one.
So one minus one is zero.
So somehow our max health of of our player game object just gonna hit zero.
So in that moment, what we need to do, we need to make the die function in order to destroy our player
game object.
For now, we're gonna print out a message, and we're gonna make the function in the bottom somewhere
over here.
So for function, we're going to type void.
And we have to name the function.
So I'm going to call it die.
Let's make first bracket and some curly brackets.
So what we want to do for now we just want to print out the message for now.
And later on we're gonna destroy our player game object.
So printing out message we're going to type debug.log.
And we will say something like this dot.
So we will say this dot sorry this dot game object dot name, dot sorry dot name.
And top of that I'm gonna add a custom message.
So let's use this plus icon and quotation.
I'm gonna say something like diet and let's close that up with semicolon.
So this dot game.
Object dot name basically it just referencing and we accessing the name.
And we can see the name is player in inspector.
We can see.
So over here this dot game object dot name just gonna be player.
And top of that we added a message says tired.
So this message should be pop up player died.
Message should be pop up in our console tab.
And we also need to call this die function.
So when we need to call this die function, once our player game object loses off all of his health.
And over here, as you can see, if the max health is already lesser than or equal to zero means our
player game object just loses all of his health.
So now we can call the die function here.
So before we return, we're going to call the die function.
If our max cell of our player game object is already became zero or less than zero.
So let's call the die function here.
And what this dysfunction just gonna do?
It's gonna print out the message.
As you can see, this dot game object dot name, basically the name of the game object and the name
of the game object is player.
And top of that we added a custom message say style.
So player type message should be pop up in our console tab.
And now we're gonna press down Ctrl S to save our script.
And we also need to call that Takedamage function as we created inside our player game object.
So we need to call.
But before we call it make sure you save your script.
So I'm gonna say I'm gonna press down Ctrl s on my keyboard in order to save my script.
Let's press down Ctrl S and let's move over to unity.
So as soon as our script just gonna compile it, we will open up our animator script, basically where
we just printing out the message.
And before we call the Takedamage function, let me show you what we simply doing.
So we will select our console tab and we will, uh, click the play button in the top in order to play
our game.
So now let's wait a few seconds.
The compilation is done by now.
So now it's done.
So let's select the console tab console tab over here.
And let's click on this play button in the top in order to play our game.
And you will see as soon as our anime two game object just gonna attack on this player game object.
The message just gonna pop up as a player takes damage and instead of printing out message, we basically
need to call the Takedamage function from this player's game.
Or you can simply say from this player game object.
So let's see before we damage it.
So let's say as you can notice that the message just getting pop up whenever this enemy two game object
attacking on our player game object.
So instead of printing out message, we're gonna call the Takedamage function.
So let's get out of play mode.
And let's double click on this message in order to move over to that line of code.
So I'm going to double click on it in order to move over to that line of code.
So just wait.
So over here as you can see we printing out message.
So instead of printing out message we're going to call the Takedamage function from our player skip.
So we basically need to go over to this player skip.
And then we need to call this Takedamage function.
So let's call that.
So let's go to animator script.
So instead of printing out message so let's remove that.
We will use this call info call info dot GameObject dot.
Uh we want to get the component and the component is player skip.
So we're going to use this dot get component.
Then make this first bracket and the component we're looking for or the script that we're looking for
is player.
So I'm going to type this player, uh sorry uh type this player and then make this first bracket.
So we do know that inside that player players keep.
We created the Takedamage function.
So after making.
First bracket we're going to call the.
We're going to access the Takedamage function.
So let's type this dot takedamage.
And let's make first bracket and then close that up with semicolon and take damage.
As you can see it's gonna take the argument which is damage amount.
So if we put five right away.
So if we put five then our player GameObject just gonna die right away.
Since we have given as five unit of health to our player game object.
So that's why it's gonna die right away.
But instead of damaging, right, basically destroying our player game object, we're gonna put one.
So every single time we just gonna attack on our player game object, we will use one unit of health.
So that's why as a damage team, as a damage amount or damage amount argument, I just pass one.
So call info dot GameObject basically means the game object the circle collided with.
So the circle that we created inside this enemy two game object, it's gonna collide with this player
game object.
So that's why this call info dot game object basically means that player game object and you we using
this dot get component in order to get the component.
And the component we are looking for is player.
So simply we just going over to this player game object and then going over to this player escape.
And from this player escape we just calling the Takedamage function.
And we do know what this Takedamage function is really doing.
It's basically subtracting our health.
But before it subtract, we just checked if the max health of our player game object is already zero
or not.
If it's a then we just calling the die function and we stop.
We stop subtracting our damage.
Basically, we just stop damaging our are basically taking damage.
We just stopping that.
And now we're going to press down control S in our anime to skip in order to save our skip.
And let's head over to unity.
So as soon as we the the compilation just gonna turn and then anime to game object just gonna place
the attack animation and it's just going to attack on our player game object.
We will able to notice a player health just gonna shrink down to one.
Uh, but we're not gonna able to notice that.
Why?
Because we maxhealth.
We made that parameter private, so that's why we not able to notice underneath our player skip, as
you can see.
But we still want to see.
So we need to move over to debug mode.
To do so go over to Inspector.
In the top right you can see three button.
So just click there and then choose this debug.
And as soon as we have chosen debug by now we can see the max health is five over here as you can notice.
So let's click on this play button in the top in order to play our game.
So, uh, so, uh, what basically, uh, enemy two game object need to, uh, damage our player game
object five times or basically need to attack on our player game object five times.
Uh, so once our max health of this player just gonna reach zero, you will see the message just gonna
pop up in our console tab.
And indeed, we can see it's became two, then one and then zero.
But still the message is not getting pop up.
But once it hits one more time, basically this enemy two game object, uh, attack on this player game
object for the sixth time, we can see the message just getting pop up in our console tab.
Player died.
So instead of checking with zero, we're gonna check with one.
Since this enemy.
Enemy two game object, uh, damaging our player.
Basically, it's taking six time in order to damage our basically to loses all of his all of this,
uh, to lose all of his health of this player game object.
So let's get out of play mode and we're gonna move over to script one more time.
So instead of checking with zero, we're going to check with, uh, one.
Sorry.
We will move over to our player script.
So as you can see we're checking with zero.
So instead of checking with zero we're going to pass one.
So now the max health is just going to be uh, so the max health of our player game object.
If it hits zero then the die function is just going to call out.
And what die function is really doing is just printing out the message.
Uh, the message says player die.
As you seen underneath our console tab, we can see player type message just gets pop up as soon as
this player game object loses all of his health.
And now we're going to press down one more time to, uh, one more time.
Uh, control is to save our script before we do.
So once we, uh, loses all of his health, as you can see, we're doing over here.
And then we're calling the die function.
So we also want to set the max health to zero.
So so in the UI, just gonna show up that the max health of this player game object is zero.
So once our max health max health of this player game object hit zero, we also want to set the max
health of this player game object to zero.
So what we will do we will set max health equal to zero.
And because we're going to show up in the UI and you will understand later on so that we just set the
Maxwell equal to zero as you can notice.
And if we just don't do it and then go and then press down control S to set our skip and then move over
to unity, you're gonna get an bug.
Let me show you that.
So that is the reason we need to set the max health to zero.
So before we set it to zero, let me show you what's gonna happen.
So this enemy two game object, this enemy two game object need to damage our player five times in order
to lose all of, uh, all of this, all of his health of this player game object.
You will see as soon as our player game object just loses all of his health.
The message just gonna pop up in our console tab says player died because we're calling the die function.
Once our player game object loses all of its health.
So the Maxwell by default we can see five.
So let's click on this play button in the top in order to play our game.
And we will notice that.
And if you just want to go over to Inspector mode then you can just go over to top right one more time.
Then click on that three dots and then click on that normal.
So you can go back to normal mode.
And the max health we can see five.
And if this enemy attacks we can see it's it's becoming four then three and then one.
And then it's becoming it's becoming zero.
But as you can see Maxwell is is stuck at one.
So that is why once this anime sorry, once this player game object loses all of his health basically
fifth time, then we also want to set back this max health to zero.
Once our player game object loses all of his health, basically this anime to game object needs a five
times in order to damage or damage or to destroy our player game object or to call the die function.
So this anime game object need to need to play the need to play five times of attack animation clip
and this anime to game object.
So let's get out of play mode.
So that's why we're gonna set back this max health to zero.
So we will move over to script one more time.
So once we call the die function, after calling that I function, I just want to set the max health
to zero, as using the max health just gets a stack at one, so that's why.
So we will set max health equal to zero.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save our script.
And now let's move over to unity.
So as soon as our our player GameObject takes damage for five times, it's gonna lose all of his health.
And you will see, after losing all of his health, this Max health just gonna set back to zero.
And we're setting it to zero because we need to show up in the UI.
So that's why we're just doing that inside our code.
And now let's play the game.
And let's take a look on this.
So as soon as this player GameObject a max health reaches zero, that function just gonna call, which
is the die function and die function.
Simply printing out the message in our console tab says player died.
And this enemy two GameObject need to damage our player GameObject five times, or it's need to attack
five times.
We can see.
So.
So the max health is became one.
And as you can see, as soon as this player GameObject loses all of his health.
We can see in our console tab the message just getting pop up saying player died and the message just
getting a pop up over again and again once this enemy to game object attacking on this player game object.
Since this player game object loses all of his health.
That's why.
And we can see the max health is zero.
So I'm gonna get out of play mode.
So I hope you get the idea that how you can make the Takedamage function, and how you can give some
health amount to your player game object.
So we basically have done the same thing as we have, as we already done with our enemy one and enemy
two script earlier.
So we also need to call the function with our enemy one.
So we need to do that.
And let's suppose you just want to go back in normal mode.
You don't want to see this max health and this private parameters.
Then go over to Inspector one more time and it's gonna say something like debug In the top right you
can see three dots.
So just click there and then click on this normal.
So you will go back to Inspector mode or normal mode.
And now we have to deal with our enemy one.
So I'm going to select my enemy to GameObject in the hierarchy.
Let's go over to Inspector and we're gonna uncheck.
Basically you can simply say we just disabling that and I'm gonna select my enemy one GameObject in
the hierarchy.
And we can check this to enable or what else you can do.
You can just go over to your project tab, a project tab assets and then prefabs folder.
Basically move over to move over to that folder in which folder you just made your enemy one GameObject
as prefab.
So make sure you you turn your game object into prefab.
So you're gonna have to copy.
And later on you can do so.
Just move over to that folder and then select your enemy.
Enemy one.
Drag it over to the scene view or into this hierarchy.
I'm not gonna do it again because I already have have a anime one game object in my scene view as well
as in the hierarchy, so that's why.
And now let's test with this, uh, test with this, uh, you can simply say this anime one.
So let's click on this play button in the top in order to play our game.
And we do know that as soon as the arrow collide with this player game object, the message just getting
pop up in the console says, uh, player takes damage.
So instead of printing out message, we're gonna call the take damage from Takedamage function from
this player's game.
Basically, we're gonna do the same thing as we have done with our enemy two.
Basically, uh, we have done recently, uh, the process.
So let's get in.
We can see if we move over to my console tab.
As you can see, the messages getting pop up player takes damage.
So instead of printing out message, what we gonna do?
Uh, we're gonna call the takedamage function from this player.
Skip.
So let's get out of play mode, and let's double click on this message in order to move over to that
line of code.
So let's double click on it.
And so over here, as you can see we just simply printing out message.
So instead of printing out message we need to call the function Takedamage function from our player
scape.
So we're going to remove this message and we will use this collision dot GameObject dot get component
basically doing the same thing.
And the component we're looking for is a player or player C script.
So we're going to pass that the component and then make this first bracket.
And we're going to call the Takedamage function by now.
So let's type this dot and then take damage and make first bracket.
And inside this first bracket it's going to take the damage amount.
So I'm going to pass one.
And if you pass a five or some bigger value then our player GameObject just gonna die right away.
And I don't want to do that.
So that's why I just pass a pass as a damage amount one.
And we're going to close that up with semicolon.
Firstly what we're doing, we're just checking if the basically if our arrow game object collided with
the player game object or not.
So this function get called when we trigger with something and we're checking if the trigger is player.
Basically collision dot game object means the game object arrow triggered with and we accessing the
tag of it, and we checking if the tag is equals equals to player basically means with the arrow just
triggering with our player game object.
Or you can simply say player arrow just colliding with our player game object.
Then what we're doing, we're just going over to player game object collision game object over here
basically means the game object the arrow trigger it with.
So arrow gonna trigger with that player game object.
So collision dot game object over here just gonna be player.
And then we're just going to player game object.
And we just going over to player script or we just getting the player script.
And from that player script we're recalling the Takedamage function as you can notice.
So basically we're just going over to this player GameObject first.
And we just getting this player script.
As you can see in the inspector.
And from this player script we're calling the Takedamage function.
Basically this function and we do know what this function is really doing.
It's basically subtracting max health uh by this amount of damage amount.
So I hope you get the idea.
And we need to save our script.
So let's press down Ctrl s to save our script.
And now let's move over to unity.
And we we will see as soon as arrow just gonna trigger with this player GameObject or just gonna collide
with this player GameObject.
Then this player GameObject just gonna loses his health.
His health by one unit since we pass our damage amount as one.
So that's why.
So let's have a look on this.
So firstly we need to get closer to this enemy one game object.
I'm going to select that anime one GameObject in the hierarchy in order to see the circle.
And let's click on this play button in the top.
So.
So if the arrow hit five times, uh, five times with this player game object, uh, so it's gonna lose
all of his health and we don't know.
We're calling the die function and die function.
Printing of the message in the console says, uh, die, which is player die.
So let's get in attack range.
And we can see indeed it's taking damage.
And once it loses all of its health, we can see underneath our console tab message.
Just getting pop up says player died.
So I hope you get the idea that how you can do it.
And I'm gonna get out of play mode.
Uh, play mode.
Uh, but, uh, we gonna make that, uh, player health public.
Why?
Uh, because we just want to adjust our player health in, uh, in the inspector.
So that's why we're going to mark that, uh, mark that parameter, uh, as serialized fail instead
of making private.
I really apologize for that because I didn't get you through that, so I really apologize.
So what we're gonna do, we're gonna mark that function as a serialized field so we will able to notice
underneath our player skip, so we can adjust the max health of this player game object.
So basically that is the thing.
So we will open up our player skip by double clicking on it.
And let's go to top of our class where we declare the variable Max health over here as you can see.
So we're going to mark this function as serialized field.
So we can see underneath our player skip.
And we can adjust the adjust the max health.
Uh there.
So we're going to mark this uh parameter as serialized field.
And now we're going to close that.
We're going to press down control S to save our skip.
And now let's move over to unity.
So basically we mark this function as a serialized field.
So we can see underneath our players grip, and we can just adjust it over here so we don't need to
go over to that script.
Basically inside our player script and change the maxhealth value over again and again to just get the
correct one.
So that's why we just marked as Celia.
And we can see health and by default we have this five.
And we need to move this move speed into our player jump title section.
Why?
Because this move speed doesn't match with our health.
So that's why we're gonna move.
Move this move speed parameter inside this player jump.
Or we can simply create another title for this as well.
So let's do that.
So I'm going to open up my player script and let's go over to uh, before we, uh, just before we,
um, split our move speed.
Let's give it a title.
So let's make this square brackets then header, and we're going to say something like, uh movement.
So let's give it a title, which is movement.
And if we press down control S and then move over to script and sorry move over to unity editor, we're
going to see a new title for this move, speed.
And the title we have given the name is movement.
So you can just put this inside this player jump title right over here.
Or you can just create a separate title for this move speed.
And we have done it already.
So we can see by now we have a movement title and the move speed parameter.
And we also have the health title and the Maxwell.
And we can adjust the max health right over here.
So we don't need to go to the script and change this value over again and again.
So that's why we just mark that this parameter as public so we can adjust underneath our player scape.
So I hope you get the idea that how you can do it.
And I'm gonna press Ctrl s on my keyboard in order to save my project.