Let's keep some health amount to our enemy game object, then we're gonna damage it.
So let's select our enemy one game object, and let's open up the.
Select the enemy one GameObject in the hierarchy first, and then open up the script by double clicking
on it.
So what we need, we need to give some health amount to our enemy game object.
So I'm gonna go to top of our class and let's declare a variable.
And that will be in like whole number 01234.
So we're going to make a public and that will be int.
And we're going to call it something like max health.
And by default you can give any value that you would like to go with.
Or you can just close that off with semicolon.
And later on you can adjust in the inspector as well.
So I'm going to give a default value something like three unit.
And let's make a function in order to take damage.
So we're going to make it somewhere in the bottom.
And we're going to make this function public.
Why.
Because we're going to call this function through another script or another C sharp class.
So that's why we're going to make this function public.
So let's make it public.
And for function we're going to type void.
And we have to name the function what we want to name it.
So we're going to call it something like take a damage.
And we're going to pass first bracket and some curly brackets.
And this function over here it's going to take an argument.
So basically what you have to do you need to write in between this first bracket int that will be int.
And we can call that one something like damage.
And we can later on we can replace this number with any number that we would like to go with something
like one, two, three, four like that.
So now what we're going to do.
So as you can see we created a Maxhealth parameter.
Now we are ready to subtract that.
So we're going to do it inside this takedamage function.
So we're going to subtract.
So max health in order to subtract we're going to use this minus equal and the amount that we want to
subtract.
So we're going to pass this damage parameter.
And let's close that up with some my column.
So let's call uh let's suppose we call this function for the first time.
So the max health by default we set three.
As you can see over here.
So this is going to be three.
And let's suppose as a damage parameter you just pass a one later on.
So this is going to be one.
So this is three.
So three minus one is going to be two.
And if you call this takedamage function one more time.
So then now the max health is two.
So two minus one is just going to be one.
And if we call this takedamage function for the third time.
So the max health is already now one.
So one minus one is just going to be zero right away.
So somehow this max health will reach zero.
So uh, zero then what we have to do, we need to destroy our enemy game object right away.
But before we destroy, we need to stop doing this thing.
So let's suppose the max health is already zero of this enemy game object.
And then we call this max health function.
So the max health is now zero, and so zero minus one is just going to be minus one.
So it will move over to negative value.
So we need to stop doing it.
So before we subtract one we need to check if the max health is already zero or not.
So we're going to check if max health is less than or equal to zero means we need to stop doing this
doing these things.
So we're going to type some curly brackets.
And in this brackets what we're going to do.
We're going to type this return and then close that output semicolon.
So after return whatever you're going to do it's not going to be executed.
So the max cell let's suppose zero.
So this condition is just going to be true right away.
And we just type in return.
And after return whatever we're going to do.
This line of code will not be executed.
Means the max health will start at zero once the max health reaches zero.
As you can see, we're doing we basically checking the max cell is already less than or equal to zero.
Then we typing return And we do know after return whatever we're going to do.
It's not going to be executed.
And let's also make a die function.
But before we do, let's save our script.
And we also need to call the function.
So we just made this Takedamage function.
And in order to take damage we also need to call that Takedamage function.
But where are we going to do where we're going to call that function?
Once this player game object shoot on this enemy game object.
At that moment, we're going to call the uh, at that moment we we will call that function, which is
the take damage as you seen.
So just wait for to compile.
Now it's done.
So let's, uh, let's select our player game object where we're shooting, and let's open up the player
script by double click on it.
So as you can see we're just printing out a message.
Uh, it says takes damage.
Uh, basically enemy takes damage.
But instead of doing this, we really want to damage our enemy.
Sorry enemy game object.
So we're going to do that.
So what we will do we're going to use this hit info parameter in order to extract info whatever the
ray hit with.
So we're going to we're going to get the component.
Firstly we will go over to transform sorry transform then Game Object.
Then we're going to use this get component.
And in this component make arrows into these arrows.
We need to pass the script name that we really want to access.
So in our case we're going to access the enemy script.
So we're going to type enemy and then make first bracket.
And over there we created the Takedamage function as used in.
So we're going to call the function over here.
So let's call it dot take damage and make first bracket and then close that up with semicolon.
So this take damage right over here.
It's going to take an argument as you can see.
So we need to give a argument.
So I'm going to pass one.
So if we pass one.
So every single time it's going to subtract one.
And let's suppose you just pass two instead of one.
Then the max health will gradually, uh gonna go down.
And if you just pass three right away then the max, uh, then the our enemy game object will, uh,
lose right away once we shoot on that enemy game object.
But I'm gonna go with one.
So that's why I just want to subtract every single time.
One unit.
If we just shoot on that enemy game object.
Uh, so we're gonna press down control S in order to save our skip.
And now let's move over to unity.
So this hit info gets to once the ray collided with that enemy game object.
Then what we're doing, we're just going hit dot, hit info dot transform dot game object basically
means the game object that ray hit with.
So the ray just definitely gonna hit with this enemy game object.
So basically we're just going over to this enemy game object transform we can see in the inspector.
And we by this transform which is accessing the whole, uh, whole this enemy game object.
And then we're just going over to this enemy script, and then we're just calling this Takedamage function,
and we don't know what Takedamage function is really doing.
It's just subtracting.
So if we call this damage function third time, and the max health of that enemy game object will gradually
down, and we also need to destroy our enemy GameObject.
But before we do, let's have a look on this.
So I'm going to select my enemy one GameObject in the hierarchy.
And let's click on this play button in the top.
In order to play our game.
You will see once we just click on this enemy game object three times, this enemy is just gonna lose
all of his health.
So first time sorry, we need to get a little bit closer.
So we're going to shoot.
So let's click.
As you can see, as soon as we shoot two times we can see Max health is now one and if I just shoot
third time we can see the max health is now zero of this enemy GameObject.
So what we need to do.
So let's get out of play mode.
So the once the uh, reach of this maxhealth, basically, once our max health of this enemy GameObject
reaches zero, then we want to destroy this enemy GameObject.
But before we destroy, let's make a die function in order to do that.
So let's go to enemy script and we're going to make the die function in the bottom.
And it doesn't need to be public.
Basically this function doesn't need to be public.
Instead we can make it private, but we can simply type type void.
So if we just don't assign with something like private right in front of this void or public, it will
be marked as a private or just gonna declare itself automatically a private function.
And we have to name the function what we want to name it.
So I'm going to call it something like die make first bracket and some curly brackets.
For now, we're gonna print out a message.
I don't want to destroy my enemy game object right away.
So that's why what I'm going to do, I'm going to type, uh, basically debug dot log, debug dot log
in order to print out a message.
And I'm going to say something like enemy.
Or we can simply pass this dot transform dot name.
And top of that I am going to add a custom message.
So plus and make quotation.
And you can just add top of that custom message that you really want to see something like uh tight.
And let's close that up with semicolon.
So this this dot transform dot name basically is just referring itself.
This just referencing this enemy game object itself.
And then we're just moving over to this transform component of this enemy game object.
And then by this transform component, we just accessing the name of the game object.
And we can see in Inspector the name is anime one, so this is going to be anime one.
The name and top of that we added a custom message.
Style means anime.
One type message is going to pop up in the console, so we also need to call this take this type function.
We just made this die function, but we never call it calling it.
So this line of code will not be executed means we're not going to able to see this message once we
loses all of our help.
So what we will do, we will move over to this update function.
And we do know that update function runs every single frame.
So every single frame we're going to keep track of how much health this anime game object kernel currently
have.
And if the max health of our anime game object is already zero, then we're going to call this die function.
And what this function is doing is just printing out a message in the console says anime one died,
as you can see.
So let's call it.
So I'm going to move over to this update function and top of this update function, I'm going to check
if I make first bracket and some curly brackets.
So what we want to check over here.
We want to check the Maxhealth of our enemy GameObject.
So I'm going to check if Maxhealth is lesser than or equal to zero means the max health of our enemy
GameObject, or this enemy GameObject just loses all of his health.
So we're checking if the max health is lesser than or equal to zero means we don't have any health,
then what we have to do?
We need to call this type function and die function.
Just going to do the best thing which which is just going to print out the message in the console says
enemy one died.
So let's call the function over here.
Once we reach a max health of this enemy GameObject zero.
So let's call the die function uh die function and make first bracket and then close that up with semicolon.
And we're going to press down control S in order to save our script.
And let's move to unity now.
So once this many enemy GameObject just gonna lose all of his health, that die function just gonna
call right away and we don't know what we doing inside that die function.
We basically printing out a message in the console says enemy one died.
So let's have a look.
So I'm going to click on this play button in the top in order to see that.
So just wait for it to compile.
And so let's basically attack on this enemy game object three times.
You will see once this enemy game object just gonna lose all of his health.
As you can see, as soon as the max health of this enemy game object, I'm going to click on this pause
button in the top in order to pause my game.
So the max health we can see in in this enemy game object is now zero.
And if we move over to console tab, we can see enemy one died message just getting pop up every single
time.
because this anime one game object just loses all of his hell.
And we do know once we lose it, we're just calling the die function and die function just doing the
rest thing.
So let's get out of play mode instead of printing out message, we just want to destroy our enemy game
object.
So let's double click on this message in order to move that line of code.
Or we can simply move over to our anime script.
So let's move to die function.
As you can see, we just printing out the message over here and after printing out message, I just
want to destroy my enemy game object.
And in order to destroy, we're going to use this destroy function.
And this destroy function, it's going to take the game object you want to destroy.
So in our case we're going to pass this dot game object basically means itself.
And then we're going to close that up with semicolon.
Firstly we're printing out message.
And after that we're just destroying the enemy game object right away.
And if you just want to put a specific time and after that amount of time, you just want to destroy.
Then as a second argument, comma, you can specify the time as well.
But I'm going to destroy my enemy game object right away.
So that's why as a second argument, I'm not gonna give any value or any time or after that amount of
time, I just want to destroy.
I just want to destroy my enemy game object right away.
So that's why this game object, I just pass.
And as a second argument, I haven't decided to put it over here.
So the enemy game object just gonna destroy right away.
And we're going to press down control S one more time in order to save our script.
And let's move to unity.
So once this enemy game object loses all of his health, you will see this enemy game object just gonna
destroy itself.
So let's click on this play button.
But before I do, I'm going to make my player speed a little bit fast.
So I'm going to select my player GameObject in the hierarchy.
And I'm going to make the speed something like 1.5.
And now let's click on this play button.
We will see this player GameObject.
Just going to move a little bit faster in the right direction because we just increased up our speed.
As you can see in the inspector underneath our player script.
So let's get rid attack.
As you can see, as soon as our enemy GameObject loses all of its health, we can see this enemy GameObject
just loses all of his all of his health.
But before it destroy it.
Also print out a message in the console says enemy one died.
As you can see.
And after that it just destroyed itself.
So let's get out of play mode.
And I want what I want to do.
I just want to, uh, basically, uh, I want to show up an explosion effect before this enemy GameObject
dies.
So we're going to do that, uh, for in order to do, I'm going to move.
Move over to my project tab, then assets and assets and then sprites folder.
Sprites folder.
I have it and I'm going to move over to my explosion folder.
So I need to take a look if there is explosion or not.
Uh, I can not able to see explosion or we can simply type over here as well.
Explosion.
And I don't see.
So we need to drag the explosion sprites first.
So what I'm gonna do, I'm gonna open up my file manager and then local Dixie, and then I'm gonna move
over to my this.
And this built.
Sorry to to to to to Do assets sprites, explosion.
So I have explosion sprites over here.
So I'm going to drag in my project in this unity project.
Uh, I'm going to make a sub folder in this sprite folder by right clicking create folder.
And I'm going to call this folder to something like sorry I'm going to right click on it.
Let's click on this rename.
And I'm going to call it explosion.
And I'm going to drag all of the explosion over here in this folder.
So let's select them and drag it over to this folder.
And unity just going to take a moment in order to uh import all of all of these assets.
So now it's done.
So what I want to do I want to show the show of the explosion.
So I'm going to move over to my explosion folder, then this and let's select this sprite.
So we need to find a perfect sprites, so this one seems perfect to me.
So I'm going to select my that sprites.
Let's go to uh, basically this sprite editor pixel per unit.
We're going to make it 16 filter mode to point no filter and then click apply and Sprite editor top
left slice.
Select automatic to create pixel count and count how many columns and rows it has.
So four eight, 12.
So we're going to put 12 columns and one row.
Click slice apply and it's done.
So let's click on this arrow and I'm going to select my first sprite.
Let's drag it over to this scene view or into this hierarchy.
And I'm going to name it to something like explosion explosion.
And we're going to make the animation fast way.
So let's select all of these individual sprite by holding down shift.
And let's move it or drag it on top of this explosion GameObject in the hierarchy in order to make the
animation right away.
Let's go to Assets Animation and I'm going to create a another folder for all of these things like uh,
explosion animation or coin animation.
So I'm going to create another folder for that, another subfolder.
So I'm going to right click new Folder.
And I'm going to call this folder to something like uh explosion uh or staff animation, something like
that.
We can call and let's open up this folder as well.
And we have to name the animation.
So I'm going to call it explosion.
And let's click on save.
And let's go to animation tab.
Click on this play button.
Now we are able to see but it's uh it seems like this is pretty much big the explosion.
So I'm gonna make it a little bit small these sprites.
So I'm going to move over to my project tab sprite sheet.
And I'm going to make this pixel per unit to something like 20.
And let's click on this apply button in the bottom.
Still, it seems a little bit bigger.
So I'm going to make something around 24.
And let's click on this apply button by scrolling down.
And now this uh, 24 seems pretty much good.
Uh, and we also don't want this animation clip to be looped.
So we're gonna, we're gonna uncheck the loop time.
So in order to do let's move over to project tab assets.
And then animation stops animation basically go over to that folder where you just place it your animation.
So in my case this stop animation sub folder and I can see the explosion animation clip over here.
And what we need to do, we're gonna uh, we don't want this animation clip to be loop.
So that's why firstly I'm going to select my, uh, that animation clip.
Let's move over to Inspector and I'm going to uncheck the loop time in order to uh, basically I don't
want animation clip to loop.
And we're going to make this explosion a game object as well.
A prefab, because we're gonna spawn this explosion once this enemy game object dies, as well as this
player game object.
So we're gonna spawn at that time, and we don't want this explosion game object to be the default in
our scene view as well as in the hierarchy.
So let's firstly make it this one prefab.
So we're going to select our explosion GameObject in the hierarchy.
And let's drag it over to this prefab folder.
And we're going to remove it from now.
And we're gonna spawn once this enemy game object dies.
So let's open up our enemy script quickly by double clicking on it.
And let's go to the top of our class.
But before we do, let's go where we're dying.
So over here, as you can see.
So before we type, we just want to show that explosion game object.
So that's why we're gonna before we do, before we destroy our enemy game object, we're going to spawn
that explosion game object or that effect.
And in order to spawn that we do know, we need to use this instantiate function and this instantiate
function as a first argument.
It's going to take the game object you want to show up.
So in our explosion.
And the second thing it's going to take the position where you just want to show up.
And the third thing it's going to take the rotation.
So let's go to the top of our class and let's declare those variable by one by one.
So let's go to top of our class.
And I'm going to make it somewhere over here.
So let's make a public.
And that explosion game object itself a whole game object.
So that's why we need to type this game object.
And we can call this one to something like explosion.
So I'm going to call it explosion.
And let's close that up with semicolon.
And make sure once you move over to unity and drag the reference or else you're going to get some error
error.
So I'm going to move over to my instantiate function.
So as a first argument it's going to take the game object or the effect that you want to show.
So in my case, this explosion.
And the second thing it's going to take the position.
So I'm going to pass this transform dot position basically means center position of our enemy game object
or the current position of our enemy game object.
And the third thing it's going to take the rotation.
So we don't we're not gonna rotate like that.
So we're going to simply pass quaternion dot identity basically means no rotation.
And let's close that up with semicolon.
And then we're going to press down Ctrl s.
And let's move to unity.
So underneath our enemy script we're able to see that field says explosion.
So what we need to do right there we need to drag the explosion game object into that slot.
So we're going to have the game object or the explosion effect.
Then we can spawn that.
So now let's have a look on this.
So as soon as our enemy script gets compiled we can see underneath our enemy script explosion.
And it says none game object basically empty.
So what we need to do, we need to drag the explosion game object over here.
So we're going to have the reference.
So let's go to assets and then prefabs folder basically where we drag it and I have it over here.
So I'm going to select that and let's drag it over to this slot.
And we can see by now we have the explosion game object as you can see.
So we can just 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 for it to compile.
And if we just, uh, attack on this enemy game object three times, you will see that.
So as you can see, as soon as our enemy game object dies, the explosion game object just shows up.
So if we move over to hierarchy, we still have the explosion game object.
As you can see the.
So we also need to destroy this explosion came object after some period of time.
And also we can see that the position of that explosion game object is a kind of bit weird.
So we need to move it a little bit down.
So let's get out of play mode and let's create an empty game object.
And we're gonna move that in the bottom of this player game object.
Because from that point or that game object or that position, we just want to show up the explosion
game object or that explosion effect.
So let's create an empty game object or in this anime game object.
So I'm going to right click on this anime one game object.
And let's create an empty game object.
And I'm going to call this game object to something like Explosion Point.
So let's call it explosion point or explosion position.
And I'm going to drag it in the bottom.
So let's select this move tool.
And I'm going to move it somewhere a fit of this player game object somewhere around here.
And I'm gonna choose an icon or gizmos.
And this seems fine.
And let's open up our anime script.
So let's open that up quickly.
So what I want to do, I want to replace this transform dot position with our explosion point as we
created.
But before we remove, we need a reference of that point that we created.
So let's go to top of our class and let's grab the reference first.
So we're going to make a public.
And that is a transform.
Basically we're just looking for position.
So that's why we're going to type transform.
And we can call this one to something like explosion point.
So I'm going to call it explosion point or position.
And let's close that up with semicolon.
And let's go to that line of code over here.
And I'm going to replace this with explosion point.
And make sure you just drag the reference in the inspector once you move over to unity.
And now what we have to do, we just created this explosion.
Basically, we spawned this explosion in this position with no rotation, as you can see.
So after spawning, we're gonna store this into another variable.
So this itself a whole game object.
So what we're going to do, we're going to store into a another game object variable.
And we can call this one to something like tame explosion steam explosion.
And let's set equal to this.
So firstly what we have done we just spawned this explosion in this point or this position with no rotation.
And after that we just we just take in that explosion, uh, basically spawn that explosion effect and
we whatever we spawn it, we just taking that.
So in our case it's just going to be explosion effect that we know.
So uh, after spawning it, we're just taking this explosion effect.
And then we're storing into this explosion a game object as you can see.
So it's gonna work like as a container.
So firstly we spawn it and then we just uh, responded.
Whatever.
We responded, we just, uh, we just take in that and then we store it into this explosion container,
as you can see, or this steam explosion game object or Tim basically referencing the temporary.
So firstly, we spawn it at this position with no rotation.
And after that, whatever we spawn it, we just store it in this steam explosion.
So we spawn that explosion effect.
So, uh, after spawning we just taking that explosion effect and then we storing into this steam explosion
container or this steam explosion game object.
And after that, what we have to do, we also need to delete that explosion container basically where
we stored that explosion effect.
So we also need to destroy the explosion effect after some period of time.
So we're going to use this destroy function in order to destroy.
So as a first argument we do know it's going to take the game object.
So you want to try you want to destroy.
So in our case stem explosion.
And we don't know, we just started that explosion effect into this stem explosion.
Or you can say you can say like in this container.
So we just also destroying that container basically where we store it that time explosion or that explosion
effect.
So we also destroying as well as you can see, but we can specify this as a second argument that time
because we just want to complete the explosion effect animation.
And after that we just want to destroy.
So that's why as a comma we're gonna specify the time.
But before we specify the time, make sure you just go to unity and see the time limit.
Basically we can move over to project tab, then assets and then prefabs folder basically where we drag
the explosion and we can see it over here.
So we can simply double click or double click on it in order to open that up in prefab mode.
And if we move over to animation and we can see the animation over here.
So something like 0.9 second.
As you can see, it's just completing the whole animation clip, as you can see in this scene view as
well.
So after this amount of time we also need to destroy this explosion effect.
So we do know we just firstly spawn it and we just store this explosion game object or that effect into
this explosion.
And after that we also destroying the explosion basically where we store it.
So as a second argument we're gonna specify the time.
So we're gonna pass 0.98.
And we do know after this amount of time which is 0.9 F, it just completely, then completing the whole
animation clip and after completing the whole animation clip, uh, we just want to destroy.
So that's why as a second argument, we just specify the time, which is 0.9 F, because after after
this amount of time, it's just completing the whole animation clip.
So that is the reason.
And we're gonna close that up with semicolon in order to, uh, just, uh, save our code.
So I'm going to press down Ctrl.
S to save our code and make sure you just close that off with semicolon as well.
So now let's move over to unity and have a look on this.
So just wait for a little moment.
So we need to go back.
So in hierarchy we can see back arrow.
So just click there in order to move in scene view.
And uh let's have a look.
But before we do make sure you select your anime game or anime one game object or the anime and select
this explosion point and drag it right there.
Because from this position we want to spawn that explosion effect.
So we need to drag the reference right there.
So we're going to have the position and then we can spawn it or else you're gonna get some weird errors.
So make sure you just drag the explosion point uh game object or this position in corresponding slot.
And now let's have a look on this.
So I'm going to click on this play button in the top to play our game to uh, basically uh to play our
game.
And I really apologize for the background noises coming out.
So let's have a look on this.
So I'm gonna click shoot on this anime game object three times 123.
As you can see, as soon as we have done that explosion game object, we are not able to see explosion
called a clone in the hierarchy as well as that game object just get destroyed.
So we also destroyed that enemy as well as the explosion effect we have shown.
We also just destroying that.
So let's get out of play mode and we need to adjust the explosion point.
Basically we need to drag it a little bit up somewhere over here.
So the explosion effect is going to be a perfect, uh, position in order to show up perfectly.
So I'm going to select my explosion point game object, and I'm going to drag it somewhere around here
I guess uh, I guess this this is gonna be perfect point.
So let's have a look and make sure you just go to overrides.
Uh, so basically select the parent game object first.
So in our case this enemy one let's go to overrides and then click on Apply All.
And now let's click on this play button at the top in order to see one more time.
So just wait for it to compile.
And once it's done we are ready to go.
And you can just play around with that and you're gonna get the hang of it.
So let's as you can see now, it's a perfectly working and it's a it's a cool thing.
I hope you get the idea.
So I'm gonna get out of my play mode.
Play mode?
I'm gonna get out of play mode.
Sorry.
I really apologize for that.
So let's have a look in the next lecture as well.
Next lecture.