We will make the attack of this enemy two game object.
So we're going to create a circle around this sort of this enemy game object.
And if that circle collide with this player game object, this player gonna take the damage.
So we're gonna basically do the same thing as we created the circle.
We're gonna create in same way around this sword.
So let's do that.
So we're gonna open up our animator script, uh, and let's make a function for it.
And we're going to make the function over here.
We.
You can make it anywhere.
Uh, it's up to you.
And make sure you just make that function public.
Why?
Because we're gonna call the function through animation event.
So if I select my animator GameObject in the hierarchy, move over to animation tab and then click over
here and then choose this Attack Animation clip.
So we're going to call the function somewhere over here.
So this is gonna be perfect time as you can see.
Uh so we're going to call the function through animation event or through animation.
So that's why we need to mark that function as a public function.
So let's go to script and let's make a public, uh, public uh, fourth function.
We're going to type void and we have to name the function.
So I'm going to call it something like attack.
Let's make first packet and some curly brackets.
So what we want to do we just want to create the circle around that sort a game object.
So we need a point from where we want to create the circle as well as we're going to need the radius
of that circle.
And then we what game object we really want to target or what uh, what game object or which game object
we really want to damage.
So in our case we're going to damage this player game object.
So we need those variable one by one.
So let's go to script.
And for that let's go to the top of our class.
And let's create those parameters.
Basically we need to uh we need all of them.
So that's why.
So first thing we need our position.
So let's create a public transform which responsible for position and we can simply call this one attack
point or attack position.
So I'm going to call it attack point.
And the second thing we need or we need to specify the radius of that circle.
So let's create another parameter that will be float.
And we can simply call this one attack radius.
And by default I'm going to give something like one unit or one app.
And the third thing we need the layer mask or which game object we really want to target.
So let's create a public layer mask and we can simply call it target layer or damage layer.
Or simply we can call attack layer as well.
I'm going to call it target layer, something like that.
And let's press down Ctrl S to save script.
And let's move to unity.
So we need to assign those variable one by one.
So the first thing we need a position from where we want to create.
So we get a error.
If we move over to console we can see we already a definition constraint target layer.
So let's double click on this.
Basically we already have a call parameter target layer as you can see.
So I'm going to call this one something else.
So I'm going to call it something like damage layer damage Layer.
And let's press down Ctrl S to save script.
And let's move to unity by now.
So make sure you just don't create a multiple parameter in the same way.
So you need to make it a little bit different than the other one.
So make sure you just do that.
And once we're done we just assign those variables one by one.
So let's create the point.
First we can see underneath our enemy two script attack point appears and it says none transform basically
empty.
So let's create a attack point first.
So let's right click on this enemy two game object.
And let's create an empty game object.
And we can call this game object to something like attack point.
So let's call it attack point.
And I'm going to move this game object somewhere in the middle of the source.
sauce.
So let's select the move tool from our scene view.
And I'm gonna move it somewhere around here.
And I'm going to choose and gizmos for that.
And now what we're going to do we're going to select our attack point.
And let's drag it over to this attack point slot in enemy tool script.
And the second thing we can see the radius and damage layer.
And it says nothing.
So we're going to choose our player layer and we assign it our player as a player layer.
So before you choose the player make sure you assign this player game object as a player layer.
As we have done with this enemy game object, basically we all we have done that thing uh, twice,
uh, as we created the big circle and now it's fine.
And let's go to script by now.
But before we do, let's go to overheads and then click on Apply All and let's go to script by now.
So let's move over to this physics, uh, basically this attack function.
And let's use this physics 2D dot overlap circle function in order to create the circle overlap circle.
And this over here, as a first argument, it's going to take the point or position from where we want
to create the circle.
So we're going to pass our attack point dot position at a point dot position.
And the second thing it's going to take the radius.
So we're going to pass our attack radius.
And the third thing it's going to take the layer mask or which game object specific specifically we
want to target.
So in our case we're going to target the player.
So we're going to pass damage layer.
And we do know as a damage layer we have chosen the player.
And make sure you just close that up with some my colon.
So this function is going to be true or false or it's going to give us a true or false depending upon
if this circle is really colliding with this player game object or not.
If it's then it's going to give us a true value and it's going to give us a false value.
So what we will do, we will store the result into a collider 2D variable because this is work like
as a collision.
So that's why we need to put it a variable called collider 2D or a container.
So I'm going to create a collider 2D container.
And we have to name the container or the variable where we want to store the result.
So I'm going to call it something like call info.
Uh sorry call info.
Call info.
Let's set equal to this.
So this call info right over here.
It can be true or false depending upon what this function returns us.
So if the circle is really colliding with that player game object.
So it's going to give us a true value.
So that's why this call info is just going to be true.
Because we're storing the result into this call info parameter as you can see.
And we directly setting it.
And if it's return false.
So this over here it's going to be false.
And let's suppose if the call info is true.
So we're going to check.
If so make first bracket and some curly brackets and what we want to check over here.
We want to check if call info is true.
Basically means the circle is really colliding with this player game object.
Then what we need to do.
We need to damage our player game object.
So we're checking if call info is true.
As you can see, since the circle is really colliding with our player game object, it means we need
to damage our player.
But before we do, let's print out a message for now.
So we're going to type debug dot log in order to print out a message.
And we're going to print out a message.
Firstly we're going to take this are gonna use this call info or call information parameter to extract
info.
We can extract info something like the name of the game object.
The circle collided with the tag and a component that we're looking for.
Anything like that.
So I'm going to use this call info parameter call info.
Then I'm going to move to that game object.
And I'm going to access the name.
And top of that I'm going to add a custom message.
So use this plus icon and then quotation.
And I'm going to say something like takes damage takes damage.
And let's close that up with semicolon.
And now this is fine.
And we can just press down Ctrl S to save our script.
So I'm going to press Ctrl S to save my script.
And let's move to unity.
So this call info.
Basically this call info we're using.
Or you can simply say this call info dot GameObject basically means in our player GameObject.
So we're gonna damage our player GameObject.
So the over here call info dot GameObject means the circle collided with the GameObject.
So the circle is just gonna collide with this player game object.
So basically call info dot GameObject means the player.
And then we accessing the name of the game object which is player.
We do know.
So this is gonna be player and player.
Top of that we added a custom message takes damage.
So player takes damage message.
Just going to pop up right away.
And now let's move over to unity.
So we need to call the function as well.
So we just made the function.
We also need to call the function.
So let's move over to animation tab.
And we're going to choose the attack animation.
And we're going to call the function over here somewhere like this.
So this is going to be a perfect time to damage our player.
So I'm going to call the function here.
Uh, so in the left side in animation tab you can see Add event.
So just click there and then choose this animation event.
Then go over to Inspector and it says no function as you can see.
So just click there and we're going to move over to anime to C Sharp Script or C sharp class.
Then we will move over to methods basically means function.
And then we're going to choose this attack.
So basically what we're doing we're just going over to this anime to uh, class or this anime to C script.
And then we just calling this function.
And what this function is really doing is just printing out this message in the console, says player
takes damage.
So now let's move over to unity.
But it would be nice if we able to see our circle.
So we're going to use the uh another function.
We do know we need to use a another function in order to be able to see our circle.
So let's go to script now.
And for that we're going to use this on draw Andrew Gizmo selected, so make sure you just include this
function in order to draw your gizmos or the circle that you want to draw.
So I already have included.
So I'm going to draw the circle.
So in order to draw we need to use this gizmos dot draw where sphere function.
And this over here as a first argument it's going to take the position from where we want to draw the
circle.
So I'm going to pass our attack point dot position.
And the second thing it's going to take the radius.
So let's pass our attack radius and then close that up with semicolon.
And I'm going to choose a color for that.
So I'm going to set gizmo dot color equal to the color that you want.
So I'm going to set color dot red something like that.
But it will give us an error.
So let's suppose you haven't dragged this attack point to this attack point slot underneath your enemy
to escape.
So this one is empty.
But over here we can see this function will try to find the attack position.
And it's going to draw this circle with this much of attack radius.
And let's suppose you didn't assign it means that field is totally empty.
Means we don't have we haven't assigned this attack position.
So that's why it's gonna give us a error.
Because it's not gonna able to find the attack position because it's totally empty.
If you haven't assign it.
So before we draw the circle, we need to make sure if we assign the attack point or not.
If if not, then we need to stop drawing the circle.
So before we draw the circle we're going to check if attack point is at sorry attack point.
Attack point is equals equals to null.
Equals equals to null.
And if that is the case let's make some curly brackets.
Means this attack point is empty means none.
So in that situation we need to stop drawing.
So that's why we're going to check.
Oh sorry.
We're going to type this return.
And we do know after return whatever line of code we're gonna write it will not be executed means our
circle will be stopped drawing.
So firstly we check if attack point is null means that attack point fill is empty.
Then we typing.
Return means we're not going to able to draw our circle, but we do know we have assign it.
So this condition over here is totally false.
So that's why the circle will be draw right away.
So now we're going to press Ctrl S to save our script.
And let's move to unity by now.
And let's have a look on this.
And we need to adjust the radius of that circle as well around our attack position.
So when I adjust the circle as how much this enemy GameObject sword is expanding.
So we if we take a look and then choose this attack animation clip so we can see we need to just increase
the size or the radius of the circle to fit around this attack.
So.
So let's select this and attack two.
And I'm gonna increase the size so this much.
And I'm gonna move this attack point somewhere in the middle of the sword.
So let's select the attack point, and I'm going to move it somewhere around here.
And we need to adjust the attack radius as well.
So I'm going to shrink down something this much I guess it's fine.
So I'm going to put 1.7 and this seems pretty much fine.
Let's go to overrides apply all and I'm going to select my console tab.
And let's click on this play button in the top in order to play our game.
And let's have a look if this circle is really colliding with this player GameObject, not if it's then
it's going to print out the message in the console.
Says player takes damage because we're just calling the function there.
So let's have a look.
So as soon as this player GameObject just gonna get inside and we can see it's just attacking and we
can see the message in the console getting pop up says player takes damage.
So instead of printing out message what we're going to do we're going to damage our player.
And we do know we just made a Takedamage function in this player game object.
So we're going to call the function in our player script.
And basically where uh, sorry, not player script in this anime script where this anime is just attacking
on our player game object.
So let's double click on this message and instead of printing out message, we're going to call the
Takedamage function from that enemy script.
Sorry, from that player or that player script.
So let's remove this and let's uh call the function here.
So we're going to use this call info parameter in order to extract information about the uh, game object
the circle collided with.
So we're going to go to call info.
Firstly we will check if call info call info dot game object dot get component.
And the component we're looking for is a uh enemy sorry player script.
So we're going to pass player and then make first bracket.
And we will check if this equal to not if that is not equal to null basically not empty.
So firstly we're just using this uh call info GameObject.
Call info.
GameObject basically means player GameObject.
So in our case player.
And so we're using this call info parameter in order to extract information.
So call info dot GameObject basically basically means the GameObject the circle collided with.
So this circle or this attack circle just gonna collide with this player game object.
So we basically moving over to this player game object.
And then we're just going over to or just getting this player script as you can see in the inspector.
So we're basically moving over to this player GameObject, this player script.
And from this player Skip, we we're just calling this Takedamage function, as you can see.
And we do know what we have done inside this Takedamage function in our player Skip.
So we basically just going as you can see and we're checking if it's not equal to null means uh the
means the player script attached uh attached to this player game object.
Sorry.
Uh, we not calling yet the Takedamage function, I really apologize.
So we're just checking if the player is script not equal to null means this player script attached to
this player GameObject.
And we can see indeed this player script is attached to this player game object.
So means the player game object is attached to player game object.
So basically this line of code basically means the player script is attached to player script.
And we're checking as you can see, if player script not equal to null means there is uh, means the
player escape is attached to our player, uh, game object.
So what we're gonna do now, we need to, uh, player game object, need to take the damage.
So we're going to use this call info dot game object.
And then we're going to get the component.
And the component we're looking for is player.
So we're going to type this player script or player class and then make first bracket.
And then we're going to call the Takedamage function.
So dot take damage and make first bracket and then close that up with semicolon.
So we need to pass an argument in this Takedamage function.
So I'm going to pass one.
Uh, so basically what we're doing, we're just going over to this player game object, uh, subclass,
as you can see in the inspector.
And from there we just calling this Takedamage function, as you can see.
And we do know, uh, what Takedamage function is really doing.
It's just, uh, it just, uh, damaging our player or we're just subtracting from our player GameObject
one unit of health.
Because as a damage parameter, we pass one, as you can see.
And now we're gonna press down Ctrl s in order to save our script.
And now let's move over to unity.
So we're going to select our player game object in order to see our max health.
So the max health we can see underneath our player skip is five inches Inspector.
So this max health is just going to shrink down once our enemy GameObject are gonna try to attack on
this player GameObject.
So let's have a look on this.
So I'm going to click on this play button in the top and have a look on this as soon as this anime game
object or this anime two or skeleton game object.
I'm gonna take a attack on this player game object.
This player game object is gonna take damage right away because we're calling the function there.
So let's see.
And as you can see, as soon as attacking we can see the max health is now zero of this player game
object.
It just loses all of his health.
So I'm going to get out of play mode.
And the message just getting pop up in the console says player type.
Because once this player game object loses all of his health, then we're calling the type function.
And in that type function we just printing out the message in the console says player type, as you
can see underneath our console tab.
So basically that is the thing.
And make sure you just select the anime to game object, then move over to Inspector Overrides and then
click on Apply All.
If anything you just change it in this anime to script in underneath your Inspector tab.
So make sure you just do that and I'm gonna press Ctrl S in order to save my project.