Right now what we want to do.
We basically want to create a circle around our attack.
So if I select my player GameObject in the hierarchy, move over to the animation tab and then select
any attack.
Let's suppose this attack to.
So as you can see we're just attacking.
So around this I just want to create a circle.
And if that circle collides with any enemy GameObject then we cannot damage it.
Since we don't have enemy.
Let's create a square for now.
And later on we're gonna replace that with enemy sprite.
So let's do it.
So what we will do first let's create that.
To create it we will move over to our hierarchy.
Let's right click or hit this plus icon.
Then go to 2D Object Sprites and then choose this square.
And let's call this square for now enemy enemy.
And make sure you go to Inspector and uh, basically uh, add a component box Collider 2D.
So we're gonna take our collision and I'm gonna to drag a little bit down.
So let's select the move tool from here in this in view.
And let's move it up here.
And let's add a box Collider 2D for collision detection.
So let's click on this add component.
And let's add box Collider 2D by searching it.
And let's hit enter in order to attach that.
And now what we will do we will assign this enemy as a enemy here.
So player just gonna detect that this is our enemy.
So we need to assign this enemy as a enemy layer.
So how you can do it.
Go over to Inspector in the top right you can see layer default.
So let's click on it.
Let's click on Add Layer.
And let's create a new layer for this enemy.
And we're going to call this enemy something like that.
And make sure you select your enemy GameObject in the hierarchy one more time.
Go over to Inspector and Layer default to set whatever you recently created.
Now we assign it this enemy as enemy layer.
Basically.
Now this player just gonna assume that this is our enemy.
So let's create the circle around our attack position.
Basically, if I select my player, move over to animation tab.
We're playing the attack animation clip.
So we will create a point up here, something over here.
And around that point we will create the circle.
And if that circle collide with this enemy then we're gonna damage this enemy.
So let's open up our script by double clicking on it.
And for that we're gonna use a function it a function.
So let's make that function somewhere around here.
You can make it anywhere you like.
And we will make this function public because we're gonna call this function through animation event.
Basically once this our player game object playing animation, we're gonna call it somewhere around
here.
Uh, we're gonna talk about it later, but make sure you just make that function public.
So that is the reason.
So we're going to make this function public.
And for function we need to use this void.
And we have to name the function.
So let's call this function something like attack.
And let's make first bracket and some curly brackets.
And in order to create a circle around that point we basically have to use this physics 2D physics 2D
dot overlap circle.
We already use this function.
Uh, I guess so basically the same way we're going to do it.
So this physics 2D physics 2D dot overlap circle is just going to create the circle.
So as a first argument it's going to take the point from where you want to create the circle or the
position.
And the second thing it's going to take the radius.
And the third argument it's going to take the layer mask basically which game object you want to damage.
So we need to assign those variable before we create.
So let's make uh, this uh comment by double forward slash.
And let's go to top of our class and let's assign.
So I'm going to do it over here.
So let's give it a title to make it nice.
So we have to type this header and let's say in title attack since this just gonna have attack.
So the first thing.
So let's create a serialized field uh private.
And this will be transform or simply you can, uh, transform.
We do know it's responsible for position and we can call this one to something like attack point or
attack position.
And the second thing, we need the radius.
So let's make a another serialized field private.
And that will be float.
And we can call this one attack radius attack radius.
And by default I'm going to set one f.
And third argument we need the layer mask.
So let's create another serialized field private.
And this will be layer mask.
Uh basically which game object we want to target so we can call it target layer or attack layer.
So I'm going to call it something like what is player A?
And let's close that off with semicolon.
And we're going to press down Ctrl S to save our script.
And now let's move over to unity.
So we need to assign those variables one by one.
So the first thing just going to pop up our title which will be attack.
And after that those variable.
So the first thing just going to pop up our attack point.
So we need to create our attack point from where we just want to create our circle.
And the second thing gonna be radius.
And the third argument is going to be uh, the layer mask.
And as a layer mask we're going to choose enemy since we want to damage our enemy game object once our
player game object, uh, attack on enemy.
So we can see as soon as our player scape gets compile attack point and it says none transform basically
empty.
So let's create attack point.
Uh, attack point.
Uh, inside this player game object.
So let's right click on this player game object and let's create an empty game object.
And we can call this game object to something like attack point or attack position.
So let's call it attack point.
And I'm going to choose an gizmos for it.
Let's go over to Inspector.
Click here and we're going to choose this one.
And let's move it.
Something over here in the center of attack.
So basically if we want to play the attack to.
So we just want to move it in center something over here it's going to be centered I guess this is fine.
And once it's fine that's pretty cool.
Now let's select our attack point and let's drag it over to this attack point field.
And the second thing which is radius.
So I'm going to leave it to one.
And what is layer uh what is layer I'm gonna choose.
Uh, this sorry I really apologize.
Instead of calling what is player.
We we basically have to call it what is enemy.
Since we damaging our enemy game object.
I really, really apologize for this.
Now I'm going to press down Ctrl s on one more time in order to save my project.
Sorry.
Save my script.
And what is enemy?
I'm gonna choose enemy.
But before you choose enemy, make sure you assign your enemy game object as enemy layer by as we have
done previously.
So you can just follow the same path.
So now underneath our attack title, we should see what is enemy.
Now I'm gonna choose enemy.
So we're gonna damage this enemy game object.
Now we assign it all of this variable and we are ready to create our circle.
So let's go to our script and let's move over to our add function.
And let's remove this two forward slash.
So this physics 2D dot overlap circle as a first argument it's gonna take the point.
So we will pass our attack point.
And we're going to access the position.
So we will type dot position.
Second thing it's gonna take the radius.
So let's pass our attack radius.
And the third argument it's going to take the layer mask.
As a layer mask, we're going to choose what is enemy.
Since we want to damage our enemy and what is enemy?
We have chosen enemy, and now we're going to close that off with semicolon.
So this physics 2D dot overlap circle.
It's going to create the circle from our attack position with this much of radius.
And it's going to damage our enemy as you can see.
So if this circle really collide that we created with one radius, with our attack radius, if that
circle really collide with this enemy game object, then this function over here, it's going to give
a true value.
Else it will give us a false value.
If the circle is not colliding with this enemy game or game object.
So what we have to do, uh, we're going to store the result into a collider 2D since this is a just
going to work as collision.
So we're gonna store it into a collider 2D variable and let's call it call info or collision information.
You can simply say, and let's set equal to this.
So this call info can be true or false depending upon if the circle is really colliding with this enemy
game object or not.
If it's, then this function is just going to be true.
So this call info will be true since we're storing the result into this call info variable.
As you can see right over here.
So what we will do for now, let's print out a message as something like says that we damaging enemy.
So what we will do in the next line we will check if.
Let's make first bracket and some curly brackets.
We want to check if our call info is true.
Call sorry call info if it's equals equals to true or we can just leave it like this.
It's basically means true means the circle is really colliding with this enemy game object.
So in this situation we need to damage it.
So let's uh for now let's print out a message.
So for printing out we're going to type this debug dot log debug dot log.
And we can say something like, uh, damaging, uh, or let's suppose say circle circle colliding,
uh, colliding with width and the name of the game object.
So top of that plus icon where, uh, we can use this plus icon in order to, uh, in order to add a
top message or basically in order to add a another message.
So I'm going to use this call info, since we are storing all of the information into this call info
so we can extract information by this parameter.
This call info parameter.
Like we can extract information the game object the circle collided with.
So the circle is just going to collide with that enemy game object.
So we can extract the name, the tag or whatever we're looking for.
So for now I'm gonna just access the name.
So I'm going to use this call info dot object dot name.
So call info Infocom object basically means the game objects the circle collided with.
So if the circle collide with this enemy game object, then this call info game object basically means
this enemy game object.
And then after that, we accessing the name of the game object.
As you can see, firstly we going over to this enemy game object and then we accessing the name of the
game object.
And we can notice that the name of the game object in the inspector is enemy.
So the message should be pop up.
In our console tab says something like circle collided or colliding with enemy.
So this message should be pop up in our console tab.
And now we're going to press down Ctrl S to save our script.
And then move over to unity and move over to unity.
And let's have a look on this.
So we need to select our console tab in order to see the message.
And once the circle just collide with this enemy game object, the message just going to pop up right
away says colliding with enemy.
anime.
So let's select our console tab.
And if we select our console tab and then 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 now if I click the left mouse button, uh, as we can see that the message not getting pop up, even
the circle colliding with this anime.
Why?
Because we just made the attack function.
We never calling it.
So if I get out of play mode, we just made the function, as you can see right over here, but we never
calling it somewhere in the code as well as in the animation event.
So we also need to call this function in order to execute this line of code.
So when we're going to call this attack function we're going to call it once we play attack animation.
Basically if I select my player in the hierarchy then move over to animation tab and then choose attack
one we're going to call.
The function is somewhere around here.
So this is going to be perfect time as you can see.
So in the animation tab left side you can see this add event.
So just click there and then select this add event.
Go over to Inspector uh Inspector in the top right and then function.
As you can see it says no function selected.
So just click there.
Then we will move over to player script.
Then methods basically function.
And we're going to add access the attack function as you can see.
So what we simply doing we just going over to this uh player script.
And then we just calling this attack function and attack function basically doing this thing.
It just printing out the message.
As you can see, if the circle collided with that enemy game object.
And we also need to call this attack function in attack two and attack three animation clip as well.
So we're going to choose those animation clip.
And let's I guess this would be perfect time.
So let's click on this Add ad, even in animation tab.
Let's go over to Inspector.
Let's go to player script methods or functions.
Then we're gonna choose this attack.
And let's do the same thing with this attack three.
Uh, I guess this just gonna be perfect time.
So let's click on this add event.
Go over to Inspector no function player.
Uh, player skip methods or function.
And let's choose this attack.
And now if we select our console tab I'm going to click on this clear in the top left to clear all of
the messages.
And then if we just go ahead and then click on this play button.
As soon as we're going to click the left mouse button, that attack function just gonna trigger through
our animation.
And once the circle collides with this enemy game object, you will able to notice that message just
going to pop up.
So if I just get closer to this enemy and then click left mouse button.
As you can see, as soon as the circle colliding with this enemy, we can see the message in our console
tab just getting pop up, say circle collided with enemy.
Once we playing the animation clip because once we playing the animation clip there, we just calling
the function, which is attack.
So, uh, so I hope you get the idea.
Instead of printing out message, we want to damage it.
Uh, for now, let's leave it.
We're going to take a look on it later.
And if we get out of play mode, uh, basically what we want to do, if we able to see our circle,
it would be nice.
And we do know that in order to see the circle or in order to draw the circle, we need to use a function
calls on draw gizmo selected.
So let's move over to our players and let's move over to this on draw Gizmo selected function.
So we will do it over here.
So we want to draw the circle around that attack point.
But before we draw we need to check if we assign it or not as we have done with this ground check.
So we will check if our attack point.
So let's use this if condition.
Let's make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check uh sorry.
We want to check if if attack point.
Attack point is not equal to null basically means we assign it our attack point.
Now that means we need to draw our circle.
And in order to draw our circle we need to use this gizmos dot draw where sphere function and this function
as first argument.
It's going to take the position.
So we will pass our attack point dot position.
Second thing it's going to take the radius.
So we will pass our attack radius.
And if you want to choose a color for it you can do it.
So before you create the circle we I want to choose a red color or something different color.
So I'm going to assign gizmos dot color.
And I'm going to set color dot red.
I want to choose red.
And let's suppose you don't assign the color that you would like to draw with.
Then it's automatically or by default will be white color.
So I hope you get the idea now.
And we're gonna press Ctrl s to save our script one more time.
And let's head over to unity.
So as soon as our player skip just gonna compile, we will able to notice the circle will be drawn from
this attack point.
Since we pass our attack point, a attack point as a basically for attacking.
So now if I select my player, as you can see, the circle just drawn from this attack point, and we
need to adjust the radius of this attack circle.
So if I select my player then move over to animation tab and select all of these attack to or attack
three Animation Clip and go over to final.
So we need to adjust around this sword or this attack area of this player game object.
So we need to increase a increase a little bit are basically the circle radius.
So we will select our player in the hierarchy.
Then go over to Inspector Player Skip and Attack radius.
I'm gonna increase a little bit something around 1.1.1.45.
So 1.45.
Or we can do 67I guess 1.5 just gonna be perfect.
So as you can see 1.5 is perfect.
And we can check with other attack animation as well.
So let's click here.
Let's choose attack.
And I guess this is fine.
And if we choose attack one.
So this seems pretty much fine.
So I hope you get the idea.
And I guess we need to drag a little bit down our attack point.
So it's gonna fix the area.
So I'm gonna choose my attack point underneath my player game object in the hierarchy, and let's drag
it a little bit down to match up perfectly.
And once you are satisfied, make sure you just go over to Inspector Overrides and then apply all.
So the changes you made, it's gonna save into this player GameObject prefab as well.
So we will have it, all of it.
And I want this animator component up.
And I want this player script in the bottom in this player GameObject.
So how we can do that?
In order to do it, you need to open up in prefab mode.
In order to do it, you can see you can just move over to hierarchy.
And if we select our player in the right side, we can see an arrow icon.
So we can just click there in order to open our player in prefab mode.
Or we can simply click on this open in Inspector right over here in the top right here.
Uh, so if we just right click on this animator component and then click on this pop up, we can see
that we cannot do it until and unless we open up this player in prefab mode.
So we need to click on this open prefab.
Or you can simply click over here in the hierarchy as well in this arrow.
Or you can simply click on this open in Inspector as well.
I'm going to click on this open prefab.
And now if I right click on this animator component and then click on this move up.
As you can see as soon as we click this animator just component, just move it up.
And this player script just gets in the bottom.
And in order to go back in hierarchy, we can see the back arrow in the top, uh, top left side in
our hierarchy tab.
So we can just click there in order to go back.
So I hope you get the idea how that how that how you can create the circle and how you can damage your
enemy game object.
And once you are done, make sure you press down Ctrl S on your keyboard in order to save your project.
Or you can simply move over to top left file and then click on the save button right over here in order
to save.
So whatever you have done in Unity editor, it will be saved.
So I'm gonna simply press down Ctrl s on my keyboard in order to save my project.