Now let's play the heart animation clip.
Once this animated enemy two GameObject takes damage.
So let's open up our enemy two script by double clicking on it, and let's move our move over to that
line of code where we're taking damage and we're taking damage.
As you can see, right over here, we subtracting from our maxhealth this damage amount, basically
we're taking damage.
So now we need to play that animation clip which is the heart.
So if we move over to unity editor and then select this enemy to GameObject in the hierarchy, then
move over to animator tab, we need to trigger this heart parameter.
As you can see, we created earlier and we do know that if we trigger this heart parameter, it's going
to play the heart.
So it's going to play the heart animation clip.
So what we need to do, we basically need to trigger this heart parameter through our code.
And we do know that we have the animation of basically we have the animator component reference.
So let's do that.
So let's move over to that line of code.
So what we will do, we will go to animator and then we're going to use dot set trigger function.
Why are we using this set trigger function.
Because that our parameter is a trigger parameter.
So that's why we need to use this dot set trigger function.
And inside this first bracket in quotation it's going to take the parameter name and make sure you just
pass your correct parameter name, which is hard in my case.
And then I'm going to close that off with semicolon.
And we're going to press down Ctrl S to save our save our script.
And now let's move over to unity.
So as soon as we can damage our enemy to game object you will see this enemy two just going to play
the heart animation clip itself of this enemy two game object.
Once it's going to take damage and once the animation is completely are completely playing.
Basically the enemy heart animation 100% clip play.
Then after it this is enemy to game object.
Just gonna play the walk or attack depending upon where player is after taking damage.
So now let's check that out.
So let's click on this play button in the top in order to play our game.
And let's have a look on it to this anime two game object.
And we can see indeed the heart animation clip is playing.
Uh heart animation is playing.
But it shortly and I really apologize for the noises.
I'm gonna give some, uh, some more amount of health to this anime two game object.
So let's select the anime two game object in the hierarchy.
Let's go to anime two script and the max.
I'm gonna give something like 100 in order to check, so that's why.
So let's click on this play button in the top right now and let's have a look on it.
So as soon as we're gonna damage our enemy two game object, it's gonna play the heart animation clip.
And once our heart animation clip completely played, then it's gonna play the, uh, attack or work,
depending upon where the player is.
So if I damage, as you can notice, it's playing the heart animation clip and after playing it's playing
the attack.
And we also need to play the death animation clip of this anime to game object.
Once this anime two game object loses his health, are loses his, loses his health.
So let's get out of play mode.
And again, I'm gonna give Max health to something around three in order to check that out.
And let's move over to our anime tools and let's move over to die function.
So we printing out that this message, as you can notice right over here.
So after printing out the message, we also want to play that death animation clip.
And we do know to play that we will go to animator component.
And then Dot.
We're going to use this set trigger function and set trigger function.
And inside this bracket we need to pass the trigger parameter.
And we can see the trigger parameter is death.
So in quotation we need to pass and make sure you spell it correct.
Uh, which is that?
And then we're going to close that up with semicolon.
And we also need to disable our box collider 2D of this anime two game object.
So it's not going to take any damage damage at all after this.
Uh, this basically this anime two game object loses all of his health.
Basically, we're doing the same thing as we have done with our anime one script, so you can follow
the same, uh, so you can just, uh, do the same way as we have done with our anime one script.
So firstly, what we will do, we will disable the box Collider 2D.
But before we disable, we will set the rigid body 2D gravity scale to zero.
So it's not going to fall down at all.
And after it we're gonna disable this box Collider 2D.
So it's not going to detect any collision.
Basically it's not gonna take any damage at all.
Once this enemy, uh, two game object loses all of his health.
So we need a reference of this rigid body 2D as well as this box collider 2D.
I guess we have the rigid body 2D reference, so let's check that out.
So we will go to our script and let's go to top of our class so we don't have the reference of it.
So let's grab it.
So first thing we're gonna grab our rigid body 2D.
So let's make it private.
And the component we're looking for is a rigid body 2D.
And we're gonna call it RB or short for rigid body 2D.
And also we need a reference to that box Collider 2D.
So we're gonna make private box Collider 2D box Collider 2D.
And we're gonna call this one box Collider 2D, something like that.
Sorry.
We're going to call it Call Collider 2D.
Let's call it Collider 2D.
And let's close that up with semicolon.
But we're getting a green underline because we already have a Collider 2D name inside this script or
somewhere.
So we can simply put a in front of this private.
We can simply put new.
So it's gonna go, uh, it's gonna go.
It's gonna vanish.
You can simply say, but instead of doing that, I'm going to name this variable to something else.
So let's call it Box Collider 2D with a non capitalized B letter.
So Box Collider 2D.
And what we need to do now.
So we made this field private.
So we're not going to able to see underneath our animator script.
But we need the reference.
So that's why we will move over to start function.
And which we gonna grab all the reference of this rigid body 2D and this box collider 2D as well as
we have done.
Basically as we grab it, our animator component, a component.
So we're gonna do the same thing.
So let's firstly do the uh, let's firstly do with our rigid body 2D.
So let's set uh RB equal to this dot, this dot, this.com object dot get component.
And the component that we want to get gate is a rigid body 2D.
So we're going to pass inside these arrows rigid body 2D.
And let's make this first bracket.
And the second component we're looking for is box Collider 2D.
So let's get that as well.
So we're going to use this dot this dot game object dot get component.
And the component we're gonna take is a box collider 2D.
So we're going to pass this box Collider 2D box Collider 2D.
And then close that up with semicolon and close that up with semicolon.
And we have the box Collider 2D pie.
Now basically we just taking this box Collider 2D.
And now let's store this box Collider 2D into this box Collider 2D variable we created.
Sorry we created up over here as you can see.
So let's store this box Collider 2D into this box Collider sorry into this box Collider 2D.
And let's set equal to this basically which simply taking this box Collider 2D as well rigidbody 2D
and we are storing in corresponding components.
We are storing rigid body 2D as you can see into this RB rb equal to this dot GameObject dot get component
rigid body 2D and we taking second.
Basically we taking our box collider 2D.
Sorry, this one is RB or rigid body 2D and this one is a box collider 2D.
We basically getting it and we storing into this box Collider 2D.
Now we have the rigid body 2D as well as the box Collider.
Basically we have this rigid body 2D as well as this box Collider 2D.
And now what we have to do, we need to set this and we're going to do it through our code.
So let's move over to our die function where we dying.
So I'm going to move over to my die function.
So let's over here.
As you can see we playing the animation clip first which is that.
And after playing that we want to disable our box Collider 2D.
But before we disable it, let's set the gravity scale to zero.
So our enemy two came object.
Not gonna not gonna fall due to our gravity.
So we will go to RB dot basically, which is going over to Rigidbody 2D.
And now we're going to access the gravity scale.
And we're going to set equal to zero f.
So it's not going to fall down at all.
And after that we're going to disable our box Collider 2D.
So it's not going to detect any collision.
So it's not going to play the death animation clip over again and again.
So after basically doing that I really apologize.
After doing that, what we're going to do, we're going to uh, we will go to Box Collider 2D and we're
going to use this dot enable and we're gonna set it to false.
Basically uncheck.
Or you can simply assume that we just disabling that.
And I really apologize.
We're gonna we're gonna set it to false.
False basically means we just disabling that box Collider 2D basically this.
And now we're going to press down control S in order to save our script.
So let's press down control S to save our script.
And now let's move over to unity.
Unity editor.
And it's going to work fine.
So just wait a little moment.
Once the compilation is done, we can just go ahead and play our game.
Firstly, we setting the gravity scale to zero.
So this army to game object not gonna fall down at all with gravity.
And the second thing we just disabling the box Collider 2D.
Once our enemy two GameObject loses all of his health.
So this enemy two GameObject not gonna detect any collision.
So it's not gonna play that animation clip over again and again.
Basically, we're doing the same thing as we have done earlier with Enemy One script.
So just wait a moment and once our compilation is done, we can just go ahead and we can play our game
and also really upload.
This is just taking a little bit longer time.
Now as you can notice where compilation is done and we can just go ahead and we can play our game.
So let's click on this play button in the top in order to play our game.
And let's have a look on this.
So just wait a moment.
And once our, uh, once our, uh, once our game is ready, then we can just go ahead and we can check
that out.
Firstly, we need to get closer to this enemy two game object, or we need to give it a moment to this
enemy two game object in order to approach towards our player game object.
So let's have a look on this by now.
So just wait.
So now if we damage our enemy, we can see it's taking.
And once it's uh, basically uh, once it's, uh, enemy loses all of his health, we can see this anime
to game object is now playing the death animation clip.
And let me show you once again, since it's a basically lagging a little bit.
So I'm gonna get out of play mode and let's click on this play button in the top one more time in order
to check that out.
And I also really apologize for just lagging a little bit.
So I guess so now let's click on this play button in the top in order to play our game one more time.
And we gonna check that out.
So let's get closer to our enemy to Game object, or let's give it a moment to our enemy to game object.
In order to approach towards our player game object, we can see if we damage first time and second
time, and then third time.
As you can see, as soon as it loses all of his health.
Result.
Now this anime two game object just state right away.
And now what we have to do?
We need to destroy this anime to game object after some period of time, once it loses all of his health.
So we will get out of play mode.
To destroy that, we're gonna use the destroy function.
So let's open up our enemy two script by double clicking on it.
And let's move over to this type function.
As you can see, firstly we playing our animation clip.
Then we setting gravity equal to zero and then we disabling the box collider 2D.
So it's not gonna detect any collision at all.
After losing all of his health.
And now what we have to do, we need to destroy it and to destroy.
We're gonna use this destroy function and this function as a first argument.
It's going to take the game object that we want to destroy.
So we want to destroy itself.
So that's why we're going to pass this dot game object basically referencing itself as a.
And the second thing comma, it's going to take the time after how much amount of time you want to destroy.
So I want to destroy it.
Something like after five seconds.
So that's why, as a second argument, I'm gonna put five F, and now I'm 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 after five seconds you will able to notice this enemy two game object just gonna destroy itself after
five seconds.
So just wait a moment and once it's done, we can check that out.
So now the compilation is done and we are ready to play our game.
So let's click on this play button in the top in order to play our game.
And let's check that out.
So now let's get closer to our game object First Army two.
And we can see one, two three.
Sorry, three.
And now loses all of his cell.
And after five seconds we can notice the enemy two GameObject just destroy itself.
And we cannot able to in the scene view as well as in the hierarchy.
And if I move over to my hierarchy, as we as you can see right over here, we're not able to notice
our enemy two GameObject after five seconds, it just destroyed it itself.
So I hope you get the idea that how you can do it and I'm gonna get out of play mode.
Basically we have that.
We basically we have done we have done the same thing as we have done with our, uh, with our enemy
pawn script earlier.
So you can just, uh, follow the same script so you can just copy and paste it, uh, inside your enemy
to escape from your enemy one script.
Or you can just, uh, follow this as well.
You can do that.
I hope you get the idea that how you can do it.