We simply printing out a message saying that enemy one died instantly.
Instead of printing out message, we need to play the death animation clip.
So if I select my enemy one GameObject in the hierarchy and then move over to animator tab.
As you can see, we have the Death animation clip.
So we need to trigger this death parameter.
So we do know that if we trigger it it's going to play that animation clip.
So what we will do we will open up our enemy one script and we will go to our update function instead
of doing over here we don't want to check every single frame or basically if we just call this die function.
So inside this die function we basically want to trigger that parameter.
So let's trigger that parameter first.
And after that we're going to take a look on that.
So firstly we need a reference of this animator component of this enemy one game object.
And we do know that we have it.
So we will simply open up enemy one script and let's go to die function.
And inside this function.
After printing out message, I want to trigger that parameter.
And we do know that as soon as we trigger it, it's going to play the death animation clip, which we
saw earlier.
So we will go to animator component, and we're going to use this set trigger function in order to trigger
that parameter.
Why are we using this trigger function.
Because if we move over to unity and we can notice that this parameter is a trigger parameter.
So firstly we are moving over to this animator component of this anime one game object.
And after that we are just using this set trigger function.
And inside the inside this brackets in quotation it's going to take the parameter name.
So the parameter name is death.
So we need to pass the death and make sure you spell it correct.
Else it's not going to work at all.
And after that we will close that off with semicolon.
And now we're going to press down Ctrl S to save our escape.
And if we move over to unity and then we loses all of our health, you will see this anime one game
object.
Just gonna play the death animation clip over again and again.
Why?
Because if we move over to our update function in update function, we are calling the die function
and we do know what we're doing inside that function.
So this type function just going to call over again and again means the death animation clip just gonna
play over again and again.
Let me show you.
So if I move over to unity and then click on this play button, you will see that once this anime one
just gonna loses all of his health, uh, than this death animation clip just gonna play over again
and again of this anime one game object.
So let me show you first.
So let's get in attack range and try to damage this anime one.
So if I damage it we can see that now it's died.
And but the problem is that as you can notice, the death animation clip just playing over again and
again.
Why?
Because if we move over to a script and then go over to update function, we do know that this update
function runs every single frame.
So every single frame we are checking if the max cell is less than or equal to zero.
If that is the case, then we are calling the die function and die function, simply triggering that
parameter which is dead, and it's going to trigger every single frame.
Since we're calling this die function inside this update function.
So that's why we're going to remove this from here to our Takedamage function.
Inside this Takedamage function we're going to move it in inside over here.
So we will get out of play mode and let's remove that.
So I'm going to get out of play mode.
And let's move over to our scape.
And let's move over to update function.
And let's remove this over here.
Instead of doing that we're going to call the die function here.
So let's call the die function here.
So we also check in right over here.
If the max health of enemy.
If it's less than or equal to zero then what we're doing we with we just stop taking.
Damage.
So we also need to call the die function once our enemy one just loses all of his health.
So we're going to call the die function here.
And let's make first bracket and then close that up with semicolon.
And now we're going to press down Ctrl S to save our escape.
And now that death animation clip not gonna play over again and again.
So since the Takedamage function we're not calling inside our update function, which update function
runs every single frame.
So that's why that function, basically that death parameter just triggering over again and again.
And we just removed it totally by now.
And it should be fine by now.
But let's have a look.
So I'm going to select my console tab.
And let's click on this play button.
And we're gonna notice a strange things underneath our enemy one script.
You will see that.
So just wait a little moment and just wait a moment.
So if we get in attack range and then attack one, we can see the maxhealth is now two.
And if we attack one more time, maxhealth is became one.
And if we attack one more time, Maxhealth is now zero.
But the problem is that if I pause my game and then move over to my console tab, we cannot able to
see that enemy one died.
Messages gets popped up.
Even though our max health of this enemy one GameObject reaches zero.
So it's still that message just not getting pop up.
But if we attack one more time, uh, time on top in this, uh, basically, if we attack on this enemy
one GameObject, then you will see that message just gonna pop up.
So I'm going to click on this pause button to unpause my game.
And let's have a look.
If I just attack one more time and now we can see the message.
So sorry.
If we attack we can see the message just gets pop up.
If I pause my game and then move over to console tab, we can see now buy now.
And you may want that message just gets pop up.
So basically what we're gonna do.
Instead of checking with zero we're going to check with one.
So that is the thing.
So we will get out of play mode and let's move over to our script.
Instead of checking zero we're going to check with one.
And now it's gonna work as fine.
It's gonna work fine by now.
So let's have a look.
So we're going to press down Ctrl S to save our script.
And now let's move over to unity.
So it should work fine by now.
So let's have a look.
So as soon as our maxhealth of this anime one just gonna reach zero, then that message just going to
pop up since we just checking with one.
So that is the reason.
So we gotten back and we just got it fixed.
So let's click on this play button in the top in order to play our game.
And let's take a look on this.
So as soon as this enemy one just lose.
Enemy one just gonna lose all of his health.
Then message just gonna pop up underneath our control tab says enemy one died and then it's going to
play that enemy one, that animation clip.
So by so if I attack three times one, two and three, we can see that if we move over to our console
tab, we can see enemy one died, message just gets popped up.
And also the parameter just gets triggered which is playing the death animation clip.
As soon as this enemy one just get, uh, loses all of his health because if it loses all of his health,
as you can see, we're checking over here.
Then we're calling the die function.
What die function simply doing is firstly printing out the message in the console says enemy one died,
as you can notice.
And after that we're just going over to animator component and we're just triggering that parameter
simply we're just moving over to this animator component of this enemy one game object and then just
triggering this state parameter.
And we do know that if we trigger it, we can see that animation just playing over again and again.
But the problem is that if we attack on this enemy one game object, still this enemy one just taking
damages and it's playing the death animation clip.
So we need to stop doing that.
So basically what we have to do, we need to disable the collider of this enemy one game object.
And we do know that if we disable the collider of this enemy one game object, it's not going to detect
any collision.
So it's gonna not gonna take any damage anymore on that takedamage function.
Not gonna call at all.
So basically we need to disable this box Collider 2D of this enemy one, basically this Collider 2D.
So we need to disable that.
So this enemy one not gonna detect any collision.
So let's get out of play mode.
To disable that we need a reference of this box Collider 2D component of this enemy one game object.
Then we can just disable that so we will open up.
But the problem is that if we go ahead and then click on this play button, as soon as we disable this
box Collider 2D manually, you will see this.
Play this.
Sorry, sorry really sorry.
This anime one just gonna fall down due to our gravity.
So if I just manually disable this box collider.
So if I select my anime one GameObject in the hierarchy, then go over to Inspector, then Box Collider
2D and then uncheck basically disable that.
Well, as you can see, as soon as we disable it, it just passes through this ground.
Why?
Because uh, that collider just not detecting any collision.
So that's why it just falls through this ground.
So what we need after disabling this box Collider 2D, we need to set the gravity scale to zero.
So if we move over to this rigidbody 2D component of this animation game object and and then move over
to gravity scale, we can see we set it to three.
So we're gonna set it to zero.
So it's not gonna fall down at all.
So we need a reference of this rigid body 2D as well as this box collider 2D.
Then we can just set that up.
So let's get out of play mode.
And let's open up anime one script by double clicking on it.
And let's go to the top of our class and let's declare those variable.
Basically we need to graph reference of those.
So the first thing we're gonna grab our box Collider 2D.
So let's make a private uh the component we're looking for is a box Collider 2D.
So we're going to type that and let's call it something like Box Collider 2D.
And it doesn't need to be public.
We're gonna grab the reference in this start function.
So that's why.
And the second component or the reference we looking for is our rigidbody 2D.
So we're going to create a private.
And the component we're looking for is a rigid body 2D.
And we can call this one RB short for rigid body.
You can simply say and now let's move over to this start function.
So this start function get call in the start of the game.
So in the start of the game we need to get the reference.
Basically we need to take this rigidbody 2D as well as this box collider 2D component of this enemy
one game object.
So let's do that.
So firstly we're gonna grab the reference box Collider 2D.
Uh you can just do it.
Uh, you don't need to, uh, make it in order in a order, certain order.
You just make it wherever you want.
Inside this start function, it doesn't need to be in order.
So firstly, we want to grab the reference of our you can simply say box Collider 2D.
So I'm going to type this.
This dot GameObject basically means it just referencing itself.
And we're going to use this dot get component in order to get the component.
And we need to make these arrows.
And inside these arrows we need to pass the component that we're looking for.
So we're looking for our box Collider 2D.
So we're going to type this box Collider 2D and then make this first bracket.
And now we're going to close that up with semicolon.
And now what we're going to do we're going to store this box Collider 2D component.
Uh, basically we're just using this dot GameObject.
Basically it's just referencing itself.
So the animation script is attached to this anime one game object.
So this GameObject, they're basically referencing this anime, one game object itself.
And we're using Dot get component in order to create the component.
So now we have this box Collider 2D component.
As you can see in the in inside our start function.
As you can see now we have it and we're going to store it into this box Collider 2D variable that we
created up here as you can notice.
So after taking that we're gonna store it into this box Collider 2D variable.
Let's set equal to this.
Basically what we're doing.
We simply just getting this box collider 2D component of this anime one game object.
And then we're storing that into this box Collider 2D variable we created up over here as you can see.
And we're gonna do the same thing with this rigid body.
So we're going to do the same thing.
So what we will do we're going to store it into this RB.
So we will set RB equal to this dot GameObject which is referencing itself.
And the component we're looking for is a component.
Sorry.
We need to type this dot get component.
And the component will be looking for is a rigid body 2D.
So we're going to type this rigid body 2D.
And let's make this first bracket and then close that up with semicolon.
So we basically just grabbing this rigid body 2D component of this enemy one game object.
And after that we storing that into this RV, that RV variable which is a rigid body 2D variable or
a container type, you can simply say you can assume that we're storing into this RB.
And we also taking our box collider 2D component.
And we're storing into this box Collider 2D variable.
As you can see inside our start function.
Now we have it this box Collider 2D as well as this rigid body 2D.
And now we are ready to set that up.
So let's move over to our die function.
Where are we?
Doing so over here.
As you can see we're triggering the parameter.
So what we're going to do now over here uh firstly we will disable our box collider 2D.
But before we disable let's set the gravity scale to zero.
So we will go to RB.
RB basically means rigid body.
We just going over to that and we're going to access the gravity scale.
And we're going to set equal to zero f.
And let's close that up with semicolon.
So we simply just moving over to this rigid body 2D component of this anime one game object and then
accessing this gravity scale right over here.
And we're setting equal to zero.
As you can see now this anime one game object not going to fall down at all.
And we also need to disable this box Collider 2D once this anime one game object loses all of his health.
And so after setting gravity to zero, we're gonna disable that box Collider 2D.
So we will go to Box Collider 2D where we store it, and then we're going to use the start.
And then we're going to use this dot enable.
And we can set equal to false.
Set equal to false basically means we just disabling that.
Or you can simply unchecking or checking if you set it to true something like that.
It's basically means checked means you just setting it to true.
Uh or you just enabling your box collider 2D.
But if you set dot enable to false basically means you just disabling that.
Or you can assume that we just unchecking or disabling this box Collider 2D.
So as soon as our enemy GameObject just gonna loses all of his health.
Firstly, we printing out the message in the console tabs enemy.
As you can see, after that we playing the anime Death animation clip, and then we setting the gravity
scale to zero.
And after that we just setting box collider 2D dot enable to false.
Basically we just disabling our box Collider 2D.
So it's not gonna take damage after.
Once our enemy won, game object loses all of his L.
So it's not going to check for any collision at all.
Once our box collider 2D enable enabled to false.
And now we're going to press down Ctrl S to save our script.
And let's move over to unity.
So as soon as this enemy one GameObject just loses all of his L.
Firstly, we're setting the gravity scale to zero, so it's not going to fall down at all.
And after that we disabling the box Collider 2D.
So it's not going to detect any collision means this enemy game object just gonna be fine.
So let's have a look.
So I'm going to click on this play button in the top in order to play my game.
And let's have a look on this.
So as soon as this enemy one enemy one GameObject Maxhealth hits zero, you will see that that animation
just gonna play right away.
So let's get in attack range and let's attack on this enemy one, two and three we can see.
And now if we just, uh, attack, we cannot see this enemy one just playing the death animation clip
by now.
Why?
Because if we select our enemy one in the hierarchy, then go over to Inspector.
We can see we just uncheck the box Collider 2D.
So that's why this enemy one not detecting any collision at all.
And also we can see if we move over to this rigidbody 2D component, we can see that we just set it
to zero gravity scale to zero.
As you can notice right over here.
So I hope you get the idea that how you can do it.
And after some period of time we also need to destroy this enemy itself.
So let's get out of play mode.
For that.
We're gonna open up our enemy one script.
So let's open that up by double clicking on it and let's move over to die function right over here.
So after some period of time we need to destroy.
And in order to destroy we need to use this destroy function.
And this destroy function in brackets.
It's going to take the game object that we want to destroy.
So we want to destroy itself or the enemy.
One game object itself.
So we're going to pass this start game object.
It's basically referencing the enemy game object itself.
And then we're going to close that off with a semicolon.
But if we don't specify sorry.
If we don't specify as a second argument the time then our enemy one just going to destroy right away
and which we really don't want to, uh, be, uh, don't want which we really don't want.
So that's why as a second argument, comma, we can specify the time.
So after something like five second I just want to destroy.
So that's why as a second argument I'm going to put five f.
Or you can simply assume five seconds.
And now I'm going to press down Ctrl S to save my script.
And let's move over to unity.
And if you don't assign the second argument then in your case the enemy one game object just gonna destroy
itself immediately, which we don't want.
So that's why as a second argument, we just pass the time.
And now we're going to take a look on it.
So let's click on this play button in the top in order to play our game.
And let's attack on this enemy one game object three times in order to destroy this enemy completely
and before it destroy.
Firstly, it's gonna play that animation clip and after something like five seconds, it's gonna destroy
itself.
So let's get in attack range.
And if we just, uh, three times, we can see by now it's played the death animation clip.
And after five seconds you will see a.
We can notice that after five seconds, that enemy one game object get destroy itself.
And we cannot see in this in view as well as as well as in the hierarchy.
So if I move over to my hierarchy, we cannot see our enemy one because it just destroy itself after
five seconds that we specified the time.
So I'm gonna get out of play mode, so I hope you get the idea.
Firstly, we're just damaging.
And once it's taking damage, we're checking if it's hit zero or something like less than one or equal
or lesser than 1 or 0, something like that.
If that is the case, then we're calling the die function and die function simply doing this thing.
Firstly, it's printing out the message, then playing the animation clip and setting the gravity scale
to zero so it's not gonna fall down at all.
And after that we just disabling the box Collider 2D.
So that's why it's not detecting any collision means even after if we disable that, then if we just
attack on this enemy one GameObject, it's gonna and it's not gonna take any damage at all since we
just disable that box.
Collider 2D, which is responsible for detecting collision, and after five seconds we destroying the
GameObject itself, as you can see.
So I hope you get the idea that how you can do it.
And I'm going to press down Ctrl s on my keyboard in order to save my project.