When we are getting closer to our player game object, we simply printing out a message in our console
tab saying as saying enemy to attack.
So basically, if I move over to my enemy two script, we simply play printing out this message say
play, play, enemy to attack, animation play enemy to attack animation.
So instead of printing out message a play enemy to attack animation instead of printing out message
what we really want to do, we basically want to play the attack animation clip of this enemy two game
object.
Basically, if we select our enemy two GameObject in the hierarchy, then move over to animator tab.
So we need to trigger this attack parameter.
We do know that if we uh sorry, not gonna trigger.
Basically we need to set this attack parameter to true.
So in that lecture animation transition lecture we have done it inside this enemy two game object.
So if you didn't watch that lecture.
So make sure you watch that lecture.
Then, uh, then you can just, uh, come into this lecture.
So we need to set this attack parameter to true.
But before we set what we need, we need a reference of this animator component.
Then we can just do that.
So let's grab the components.
And also we're gonna set this attack parameter to true.
So let's do that.
So we're going to open up our animator script by double clicking on it.
And let's go to top of our class.
And let's make it over here.
So that parameter doesn't need to be public.
So we can make it private instead.
And the component we're looking for is animator.
So we're going to type this animator.
And we have to name the uh we have to name the parameter.
So I'm going to call it something like animator with small letter with a with a non non capitalized
a letter as you can see.
Now let's move over to this start function.
So in this start of the game we're going to grab that.
So we will use this this start this start game object dot get component.
And let's make this arrow.
So we're going to pass inside this arrow which is animator.
And then make this first bracket and then close that up with semicolon.
Now so we just simply getting the component which is animator that we're looking for.
Now we have the component as you can see.
Now let's store this animator component into this animator variable.
So store that animator component into this animator variable.
So let's set a animator equal to this dot game object dot get component.
So this dot game object basically is just referring itself.
And we're just getting this animator component.
And after getting it we're storing this animator component into this animator variable.
As you can see right over here we are saying animator equal to this dot game object dot get component
animator.
Now we have the reference by now and we are ready to set that.
So we need to set our attack parameter to true.
To do that, we will move over to this line of code over here, as you can see.
So also we are printing out this message in our console tab says like anime play anime to attack animation.
So instead of printing out message we need to set that attack parameter to true.
So we will go to animator component and we're going to use this set bool function because that parameter
is a bool.
So that's why we need to use this set bool function.
And then make this first bracket.
And inside this quotation as a first argument it's going to take the parameter name which is attack,
and make sure you spelling it correct.
And the second thing comma it's going to take the value.
So we're going to set it to true basically means take.
And we do know if we check or set it to true.
It's going to play the attack animation clip of that anime two game object.
And now we're going to close that up with semicolon.
And let's press down Ctrl S to save our script.
And now let's move over to unity.
So as soon as we're gonna get closer to our player game object, and this enemy to game object will
get closer to this player game object and this enemy to game object.
Just gonna play the attack animation clip once it's get too much closer to this player game object.
So just wait a moment.
Once the compilation is done, we can just go ahead and we can play our game in order to check that
out.
So just wait a moment.
So once it's done, we can just go ahead and we can just play our game in order to check that out.
So now wait for a little moment.
So we simply instead of printing out message by now, we simply just setting that attack parameter to
true.
As you can notice any animator tab.
And we do know that if we just check that or set it to true.
This anime to game object just gonna play the attack to animation clip.
So let's click on this play button in the top in order to play our game.
Let's take a look on it.
So just wait a moment.
So now let's get with chasing.
And as soon as we get closer as you can notice that this anime two game object just playing the attack
animation clip over again and again, again and again.
But if we just get outside of this attack range, we can still able to notice that this enemy two just,
uh, playing the attack animation clip over again and again and it's playing forever.
It's not going back to our work animation clip to go back and work animation clip.
We do know that we need to set this attack parameter to false.
And we do know that once we're gonna set it, it's going to play the play the work animation clip.
So let's get out of play mode and let's set it through our code.
So let's move over to our scape.
And over here.
As you can see we chasing our player game object right over here.
So when we are chasing we need to play the play the work animation clip instead of attack.
So that's why we're going to copy this line of code.
Basically we will go over to animator animator and we're going to use this set bool function.
And as a second second argument we're gonna set it to false or basically means unchecked.
And we do know once we're gonna uncheck or we're gonna set it to false, it will play the, uh, it
will play the work animation clip instead of the attack one.
Uh, so let's, uh, pass our value, which will be false.
And now we're going to press down Ctrl s to save our script.
And now let's move over to unity.
So as soon as we're gonna get out of attack range or from this enemy to game object, you will see this
anime two game object.
Just gonna play the work animation clip right away instead of playing the anime to attack animation
clip.
So just wait a little moment and once it's done, we can just check that out.
So now if I just, uh, go ahead and click on this play button in the top in order to play our game.
Let's take a look on this.
So just wait a moment.
So whenever we're gonna get outside of that attack range, this anime two game object just gonna play
the attack.
Uh, sorry.
Work animation clip.
As you can see, indeed it's playing.
And when we go to the right side, we can see it's now playing the animation clip as well.
But the problem is that I want this anime two game object to stop a little bit further away from this
player game object, because we can see that this, uh, this, uh, basically the weapon just getting
outside of the player or overlapping with the player.
I want to be something over here.
So that's why we need to stop our, uh, enemy two game object a little bit further away.
Basically, we need to just bump up the retrieve distance between this player and this enemy to game
object.
So let's get out of play mode and we will select our enemy two GameObject in the hierarchy.
Let's go over to enemy two Script in Inspector, and we're going to increase the retrieve distance.
So let's set it set the retrieve distance to something like 4.5.
And let's click on this play button.
And we can check that out in play mode as well.
And once we get out of play mode we can just remember the number and we can put it back.
So just wait a moment and we can check that out.
So now it should work fine.
If it's not we can just do it in, uh, do it in this enemy two.
As you can see, this seems pretty much cool by now.
uh, if you want a little bit further away, then you can just increase the retrieve distance so we
can do something like 4.6, I guess 4.6.
Sorry, 4.6.
Now we can see it's pretty much fine.
So 4.6 seems a perfect number.
So let's get out of play mode.
And let's put the number back to back from this 4.5 to 4.6.
Because if you do any changes in play mode it's not gonna save at all.
So make sure you just remember the number.
Or you can simply right click on the component.
Then click on the scope copy component.
And when you get out of play mode, then right click on it and then paste the component value, uh value
by right clicking you're gonna get that.
So I hope you get the idea.
And now what we need to do, we need to give some health amount to this enemy to game object in order
to take damage.
And when we're gonna take damage, then this enemy two game object need to play the heart animation
clip.
So we need to before we damage it.
We basically need to give some health amount to this enemy to game object.
So let's open up the enemy two script by double clicking on it.
And let's go to the top of our class.
And over here, uh, let's give it a title first.
So we're going to make this header header.
And let's call this something like health.
So let's call it hell.
Uh, hell something.
Sorry I misspell it.
Hell.
And now this is fine.
So let's make a serialized field.
So let's make a serialized field, uh, private.
And that will be int, like, whole numbers zero, one, two, three, four.
And we can call this one something like Max health.
And by default I'm gonna give something around three unit of health.
And now let's make a function in order to take damage.
So we're going to scroll down to the bottom and let's make it somewhere over here.
It doesn't need to be in order.
You can just make it anywhere you would like to go with.
You can just do it.
So we will make this function public, because we're going to call this function from another class
or another script.
That's why we're gonna make this function.
Or we're gonna mark this function as public.
So for public and for function we're gonna type void.
And we have to name the function.
So I'm going to call it something like take damage take damage.
And let's make first bracket and some curly brackets.
So this take damage in arguments.
It's going to take an argument which will be a damage amount.
So let's pass.
And that will be int like int type like 01234.
And we can call this one damage amount.
Basically we're doing the same thing as we have done inside our player skip.
So if I move over to my player skip we already created a take damage function.
I guess if it's not sorry we have done inside this enemy to animated game object.
Sorry anime one I really apologize.
Anime one as you can notice right over here.
So we're gonna do the same thing.
So you can simply copy this line of code by Ctrl C and you can just paste inside your anime script,
but I'm gonna do it from scratch to explain it better.
So damage amount.
So before we damage, we need to check the health of our anime to game object.
It's already zero.
Then we don't need to damage it.
Damage after that.
After that we don't want to damage.
So before we damage, we just want to check.
If so, let's make first bracket and some curly brackets and what we want to check over here.
We basically want to check if our maxhealth of this anime two game object, if it's less than or equal
to zero.
If that is the case, then we don't want to damage at all, damage or damage at all.
But if the max is not less than or equal to zero, basically means that enemy two game object is still
have some health amount, so else condition else.
If the enemy to game object have some health amount, then we're gonna damage it.
So that's why from this maxhealth we're going to subtract minus equal our damage amount.
And later on we're gonna replace this damage amount with something like one.
And if the max health reaches zero then we need to stop subtracting.
So that's why we're going to type this return.
And we do know that after return, whatever we're going to do it not it's not gonna be execute at all.
So the max health we subtracting damage amount as you can see.
So let's suppose we call this Takedamage function for the first time for the first time.
So maxhealth by default we have given three.
If I scroll up, as you can notice maxhealth I already given three.
So this one right over here, the max health just gonna be three.
So the max health is not less lesser than or equal to zero.
So it's going to jump over to this else condition.
And what we're doing inside this else condition we just subtracting our damage amount.
And let's assume as a damage amount we just pass one.
So the max is three.
And this damage amount is one.
And if we call this takedamage function so the Maxwell is three and the damage amount is one.
So three minus one two.
And if we call this takedamage function for the next time.
So the max health is now two.
So two minus one is going to be one uh two minus one gonna be one.
And if we call this take damage for the third time.
So the Maxwell is now one one.
And this damage amount we assume it one.
So one minus one gonna be zero.
So somehow our max health of this enemy two game object just gonna reaches zero.
And once it reaches zero, as you can see, if Maxwell is less than or equal to zero, then we typing
return.
Return basically means after return, whatever we're gonna do, it's not gonna be executed at all.
Basically means our max health.
So this line of code will not be call out means our max.
Hell, just gonna start at zero.
So Max health just loses all of his health.
So now what we have to do?
We're gonna make a die function in order to print out the message.
So let's make a die function.
So I'm going to make it over here.
And you can assume that basically we're doing the same thing as we have done inside that enemy one script.
We're doing the same thing or we just approaching in the same way.
So for function we're going to type void.
And this function doesn't need to be public or private.
So this function just gonna automatically marked as a private function if we don't assign a don't assign
in front of this void, something like private or public.
So this function automatically just gonna marked as a private function.
And we have to name the function.
So I'm going to call it die.
Let's make first bracket and some curly brackets for function.
For now, we're going to print out a message something like this.
Anime two game object dice for printing out message I'm going to type this Debug.log and we're going
to say this dot game object dot name.
And top of that I'm going to add a message custom message.
So you need to use this plus icon and this quotation.
And you can just add up the custom message that you want.
So I'm going to say something like diet diet.
And let's close that up with semicolon.
So this dot game object dot name basically means the game object referencing itself.
And the way we accessing the name.
So this enemy two script is attached to this anime two game object, as you can notice in the inspector.
So we accessing the name.
So the name we can see is anime two.
So right over here the name just going to be anime two.
And top of that we just added this message says type.
So anime two enemy two diet message should be pop up in our console tab.
So we also need to call this function.
Once our maxalt of that enemy two game object reaches zero.
And over here, as you can notice, we're checking if the max cell is already less than or equal to
zero means our enemy two game object just loses all of his health and all of his health.
The max health is now zero.
So we are we are ready to call the die function here, because enemy two game object just loses all
of his health.
So we're gonna call the die function here, and let's make first bucket and then close that up with
semicolon.
And we're going to press down Ctrl S to save our script.
And let's move over to unity.
So as soon as we're gonna start taking damage from our player game object this enemy to enemy to health
just gonna reach zero.
But the problem is that we're not gonna take any damage at all.
Why?
Uh, because we just made the Takedamage function, and we just never calling it anywhere.
So we also need to call that function.
But before we call, let me show you.
So the max cell, as you can see underneath our enemy two script is held as we just given as a default
value.
So the Maxwell is three.
And if we just go ahead and then click on this play button in the top, you will see this enemy two
game object.
Not gonna lose any of his health.
Why?
Because we that made the function take damage and we never calling it.
So we also need to call.
So let's take damage from this our player game object.
You will see uh so if I damage my player enemy two game object.
As you can see, the max health underneath our enemy two script is stuck at zero and it's not going
down.
So we we do know that we just made that function, which is take damage and we never calling it.
So we need to call it.
So let's get out of play mode where we need to call it once this plays attack animation clip basically
once our player GameObject plays attack animation and the circle just collides with this enemy two GameObject.
At that moment we're going to call it.
So if I open up my enemy two script.
So as you can notice right over here, we just made the function and we never calling it.
So that's why this line of code not getting executed, basically we're not taking any damage at all.
So we also need to call this Takedamage function.
But when we need to call it once our player GameObject, this player game object is attack on this enemy
to game object.
So let's open up the player script where we attacking.
So we will select our player GameObject in the hierarchy.
And let's open up the player script by double clicking on it in the Inspector.
And let's go to attack function where we attacking.
So right over here, as you can see we attacking right here.
So we firstly just checking.
So as you can see we damaging our enemy one game object once the circle collided with that enemy one
game object.
So we're gonna do the same thing over here as well.
But before we do, make sure you assign your anime to game object as a anime layer because we just damaging
our anime layer, so that's why.
So if I select my anime to game object in the hierarchy and then go over to Inspector, we can see the
tag, we assign it anime, and also we assign it this anime to game object as a anime layer.
So now it's basically this anime two game object is ready to take damage.
So now we need to call the function.
So before we call the function we need to check if the circle is really collided.
So if I select my player game object, as you can see we created this circle.
And if that circle really collide with this anime game object, then we only then at that moment we're
gonna call the Takedamage function, else we're not gonna do it or at all.
So before we damage our enemy to game object, we need to check if this circle really collided with
this enemy to game.
Object or not.
If it's then this enemy two game object need to take damage as it shouldn't.
So that is the thing.
So that's why before we damage we gonna check so we can just use this else if.
Or you can just make this if condition as well.
And then make the first bracket and some curly brackets.
And you can put else if as well.
So let's do it.
Else if sorry we're gonna do that else if else if.
And we need to pass the condition over here in brackets.
So what we want to check.
We basically want to check the game.
The game object that we damaging.
If that game object has a script called enemy two script or not.
So we're going to type this call info dot game object dot get component.
And the component we're looking for is enemy two.
Since we want to damage our enemy two game object.
So that's why we need to access the enemy two script.
So we're going to pass our anime to and then make this first bucket.
And we will check if the script is not equal to null.
Basically means this script, this anime to script is attached to our anime, this anime game object.
Firstly what we're doing, we're just going over to that game object.
So call info dot GameObject basically means the game object the circle collided with as.
So the circle is just going to collide with this enemy to game object.
Then what we're doing, we're just getting the anime to script.
So we're just going over to this anime to script.
So firstly we're just going over to this anime to game object.
And we accessing this anime script and we checking if it's not equal to null.
It's basically means that this anime script is this anime two script is attached to this anime two game
object.
And indeed we can notice this anime two script is attached to this anime two game object.
So now we are ready to damage.
So we need to call the Takedamage function from that animated script by now.
So?
So that's why we check before we damage.
So we just checking if the enemy two script is attached to that enemy two game object or not.
So now we are ready to damage.
So to damage that we will go to call info dot GameObject basically means we just going over to that
enemy, uh to GameObject.
And now we're gonna use this dot get component and make these errors.
And inside these errors you need to pass the component that you are looking for.
So we're looking for enemy to script component or enemy to subclass.
So that's why we're going to pass this enemy to enemy to see script then make this first bracket.
And now we need to call the function that we created inside that enemy two function.
Uh sorry enemy two script.
So the function dot.
So we're going to type this dot and let's call the Takedamage function.
And let's make first bracket and then close that up with semicolon.
And as you can notice this take damage.
It's gonna take, uh, the argument, which is damage amount.
So if you just pass three right over here, then enemy just gonna.
Enemy two, game object.
Just gonna lose all of his health right away.
And I don't want that.
Because since we have given to this enemy two game object the Maxwell, as you can notice right over
here.
Three.
So I don't want to destroy my enemy to game object right away.
So that's why as a damage parameter or damage amount I'm gonna pass one.
I just want to subtract by one unit.
So that's why.
And now if we press down Ctrl s to save our skip and then move over to unity editor, it's gonna be
it's gonna work fine.
So firstly, what we're doing, we're just going over to this enemy two script, as you can see.
And inside this enemy two script, we can notice that we just made this Takedamage function.
So we're just calling this function there once our player just attacking on enemy two game object.
As you can notice right over here.
Firstly we're just going getting the enemy to script and then we're just calling the function from there.
And now it's gonna work fine.
So let's move over to our unity editor and let's check that out.
So I'm going to click on this play button.
And by now you will be able to notice this enemy two game object.
Just gonna able to take damage.
So just wait a moment and you will see that.
So the max health by default we have given three.
So let's get closer to this enemy two game object and let's damage it.
Now we can see the max health is two.
And if we damage one more time the max health is now one.
And if we damage one more time, the max health is zero.
But the problem is that once our max health reaches zero, the message not getting pop up pop up.
Because if we move over to our script and move over to our enemy two script, as you can see, if the
max cell Maxwell is less than or equal to zero, then we call it the type function.
And this function simply printing out this message saying enemy two died.
But the.
But the problem is that once our Maxwell reaches zero, we still not able to notice that that message
just getting pop up or that type function.
So what we gonna do to, uh, just, uh, to fix these bugs, we will get out of play mode instead of
checking with zero, we're going to check with one.
So it's going to be a one more ahead unit.
So that's why we're going to check right over here.
If the Maxwell is lesser than or equal to one.
So now the function should be pop up.
So let's press down Ctrl s now and let's move over to unity.
You will see one once.
We just gonna attack on uh on into this enemy two game object on uh, on to on.
Sorry.
Uh, once we're gonna damage this enemy two game object three times, you will see the message that's
going to pop up says enemy two died.
So let's see that.
So make sure you select your console tab.
And let's click on this play button in the top.
And let's damage this enemy two game object three times.
So as soon as our max health of this enemy two game object just reaches zero, then die function just
gonna call out and we do know what we're doing inside that die function.
We simply printing out enemy two game object died.
So if I just get closer to this enemy two game object and damage it.
So the max is now two.
And if we damage one more time, max health, we can see one.
And if we damage third time, we can see by now the max health just getting stuck at one.
But we're getting a message.
So if we move over to our console tab we can see a message just gets pop up.
Says enemy two died.
So basically it's working fine.
So instead of checking with zero, you can check with uh, you can check with, uh, you can simply
say you can, uh, check with one.
And I also want to set the max cell value to zero instead of setting it to one.
So what we will do, we will get out of play mode.
And then we're going to move over to our script.
And once our max cells become less than or equal to one, I also want to set the max cell to zero.
So what I'm going to do I'm going to set max health, max health equal to zero since we're going to
show this up in the UI.
So that's why we're doing it.
And now if I press control S on my keyboard and then move over to our script, you will see once we
just gonna attack on this, uh, enemy two game object three times, it's just gonna lose all of its
health.
And this max health is just gonna be zero right away once it loses all of his health.
So let's have a look on this as well.
So let's click on this play button in the top in order to play our game.
And let's check that out.
So once our max is just gonna be zero of this anime.
Two game object.
Then the message just going to pop up because we're calling the die function and die function.
Simply printing out the message in the console tab says anime two die.
So let's get closer and let's attack three times.
One, two and three.
We can see the message just gets pop up.
Anime two died in our console tab.
And if we move over to Inspector by selecting this anime two GameObject in the hierarchy, we can see
the Maxhealth.
We just set it to zero.
As soon as this anime two game object loses all of his health.
So I hope you get the idea.
And also, we need to play.
Uh, basically we need to play the heart animation clip, uh, heart animation clip of this anime to
game object.
So so it's gonna play that animation clip.
Basically we need to play that once our anime two game objects takes damage from this player game object.
At that moment we need to play that animation clip.
So I hope you get the idea and I'm gonna get out of play mode.