Welcome back, My favorite game developers.
In this video, we are actually
going to be melee attacking.
So the way we're going to do this is
we're going to create a method for the attacking.
And here we are going to create
an overlap circle around a certain point.
We are going to make sure to call
this defense system every time we swing a sword.
So without any further or do,
let me just show you what we've done in here.
So the enemy gets up.
He does once, damage twice, three times,
and you notice that he only damages us when we
actually get hit on the head with the sword.
So let's not waste any more time.
And let's get started.
O k.
So now let's go ahead and create the Malay attack.
Obviously right now,
the boss just swings around the gone,
I mean, sorry the sword.
He swings it around but he
doesn't actually do any damage.
So we are going to do an actual damage.
So I'm going to first of all
explain to you how to make a attack.
So obviously there are many,
many ways of creating melee attacks,
but this is the way that we
are going to be implementing it.
Sorry for that.
And it's the way that we will learn most out of it.
So the first thing we'll do is we'll create a circle of
damage to the player from a certain position.
So every time the player
sort is in this position in the animation,
we will create a circle right here,
or an overlapping circle,
which you will know later on that we'll check
if the player is inside the radius of the circle,
he will get damaged.
If he's not, if he gets away in time,
you will not be damaged.
So we'll make sure that we will use something called the
physics 2D overlap circle whenever the pulse attacks.
So remember physics to the overlap circle.
And let's go ahead and create
a couple of variables to do just that.
We are going to be handling
this attack right here from the script.
So let's go ahead and get started.
The first thing that we are going to
do is create a serialized field.
And let's go ahead and make this one
private because we already know what it does.
So ent damage amount
and it's going to be equal to 20 as a default.
Then we'll need to create
a transform which will represent the point of attack.
So point of attack.
And why do we have a point of attack?
Because if I take you back right here,
you'll see that we have
a certain middle and this will be the point of attack
around which we will build
our circle and check for the player inside of it.
So we're going to obviously
just divert create that game object.
But before we do that,
will obviously also need something called a radius,
which is the attack radius.
So not to be confused with the and attack range.
So the attack range is
the range at which we will start attacking.
The attack radius is the radius in
which the player gets damaged if he stays there.
And then we are going to create finally
a serialized field of type layer mask.
And it's going to be called what
is player and why do we call it like that?
Well, I, since I started using Unity,
all the forums, YouTube videos,
other courses, and generally the Unity community always
caused what is player whenever
we want to set some kind of layer.
So, and not only the player,
sometimes it's called what is ground.
So I, I am used to it.
You can call it whatever you want,
but this is the general consensus on this.
And before we move on,
we need a way to actually see that radius.
So we're going to use the void onDraw gizmos selected.
We've done this already.
Nothing too a fancy in here.
So gizmos color, color red.
And let's just close this off. Hit Enter.
And here we are going to go to
the gizmos draw wire, sphere.
We're sphere and it's going to be on
the point of attack dot position.
And it's going to be the attack radius as the radius.
So there we go, The andro sphere,
as you can see, a center and a radius.
We just gave them the point of
attack position, attack radius.
So back in our game.
And do we need to wait for anything to compile?
Know that was fast.
So I'm going to right-click and create
an empty game object as a child of the boss.
And we'll call it the point of attack.
And on these Kelly boss down here,
I'm going to add the point of attack.
What is the player?
It is the player and the attack radius.
I'm just going to go into the scene to
see what is going on. And there we go.
Obviously, as we always do,
this will be up to you.
Where do you want to set your animation or,
sorry, I was thinking of the animation.
Where do you want to set the point of
attack and how big do you want to make it?
So I'm going to scroll down and see the attack.
So in the attack, there we go.
This is the point where we are going to attack,
so exactly on here.
And from this point we will be able to determine
how much do we want to move the radius.
And notice that because
the onDraw gizmos is on the bus controller,
that means that we
increase in here just a little bit, just make it too.
I think two is an appropriate number,
but I'm going to go ahead and make it three.
So just for testing purposes.
So I'm going to move it a bit Skelly.
And then if we see another attack in here,
so we haven't, There we go.
If the player is standing anywhere inside of this radius,
he will get had.
So without any further ado,
let's save that and let's go and issue you a challenge
because this time you are going to do the attack.
First of all, you need to create
a method and the boss controller make it public.
Why? Because we need it somewhere else.
Then we are going to check for
the overlaps circle in the unity dogs.
This will be your job.
You'll need to go see how everything works.
And you need to check if
the player is inside the attack radius.
If he is, then we will apply damage.
Call this method using an event animation.
So with that out of the way,
remember, let me just show you something.
It's called the physics to the overlap circle.
And now pause the video and go ahead and do the Shalon.
Oh, okay, welcome back.
So in here I'm going to go ahead first
of all and create a public void.
And I'm going to call it the attack layer.
Great.
Now, what are we going to do in here?
First of all, we will need to understand
how the physics 2D overlap circle works.
So let's say I am a clueless game developer.
I have no idea how physics to the overlap circle works.
So first of all, I just check in here.
So the declaration, a public static,
no need to worry about the static.
We can do it without the static.
I scroll down in here.
I can see that it's a collider 2D overlap circle.
We have a point, we have a radius, a layer mask,
I mean depth, and a max depth.
So this is for the z
coordinates when we are working in 3D,
I presume, or maybe something includes object O,
objects that are in the z.
So it also checks for the depth.
We will not be needing this.
If we scroll down, we can see that we can actually
also get some kind of results in here.
And we can also use contact photos
being learned about contact filters in one of the videos,
we get a, an array,
but let's also scroll down.
Okay, So that's it, that's all we need.
We can also return lists.
So this is what we need and this returns a collider,
2D collider overlapping the circle checks
if a collider falls within the circular area.
The circle redefines coordinates, etcetera, etcetera.
And in here you can go more
in-depth into the z coordinates.
But anyways, this is what we need physics,
the overlap circle back in our game.
What do we want to do in here?
First of all, we'll need to get
the player that we are attacking.
So I'm going to create a collider 2D.
And it's going to be the player to attack.
And I'm going to say that he is equal to
the physics 2D dot overlap circle.
And I'm going to say, what do we have in here?
So first of all, we have the point.
What's the point? Obviously, it's
the point of attack, that position.
Then we need the radius.
So it's the attack radius.
And then we need the what is Player because
we only want to be hitting the player.
Then in here we are going to check if the player
to attack layer, layer.
So, okay, so player to attack is different than null.
So why do we say that he is different than null?
If we look in here, Let's save that.
That's first of all,
print out the player to attack.
So save that.
And let's go and to regain,
and let's add it to the event system.
So where do we want this to be called?
If I go into the attack in here,
I swing the sword and just as he swings and sold,
I'm going to apply the function.
And which function should we apply?
At that layer.
And then again, when we swing down in here,
we're going to add a,
another option, sorry, I mean, another event.
And we are going to attack the player.
So save that, run the game.
And let's see what we mean by null.
So let him attack us.
So there we go, doom player, do layer.
There we go. If we swing, There we go.
We can see that we have a null
in here when we were outside.
So because the way we are going to continue,
because we are going to get the player to attack that get
component though the player health handler.
And what was the method than there?
I believe it was damage
the player and the amount of damage,
the damage amount that we've created up here.
So it seems that I hope you understand what we didn't.
If we don't have or we
don't check for the player is null.
So we are going to access the player health handler,
which does not exist,
which will cause an error.
So we have this extra step of safety.
We check if the player to attack is different than null.
If it is, then we attack or actually damage.
If it's not, that means he is outside the radius.
That means we don't.
Kim and I'm going to keep this
in here just for testing purposes.
So let's save all of that.
And let's go back into our game and here,
and let's run and see what is going to happen.
So the player gets close, he fights,
and as you can see,
he takes damage every single time.
So I'm going to make the dunes player max
help a bit bigger. Max health.
Let's make it 500 for the testing purposes, run the game.
How cool is that, that we can change
the life every time we want,
because we've created the game.
So there we go, 202020.
There we go. You can see that we get hit.
We get it.
We get hit every single time.
We remove or get our lives reduced by 20.
And you might be thinking, well,
why aren't we getting hit on the swing down?
Well, let me show you.
So I tried to get hit.
Second. Get hit.
Okay, So that didn't work well because we
are a bit invincible in a couple of seconds.
So you can see,
let me just try to do this.
I know it's not working.
So let me see if I can remove the sensibility time to 0.
And that way we won't become invincible.
You'll notice that we get
hit the way up and on the way down,
on the way up and on the way down.
And as you can see, this is why we
added a bit of invincibility to our player
because this is not very fun
to have getting damaged every single time.
And if we keep getting damaged, eventually,
the player is going to die to succumbed to his wounds.
So you can see right here we have
a couple of errors because of the on-state Enter.
We are trying to find the player,
but there is no layer.
Now, we could either keep this.
Obviously we should not keep
this because this is an error.
This might cause problems,
might cause couple of lines to not be taken into account.
So a vacua in here is to
just check if he is available and so on and so forth.
But let's not worry about that.
Let's worry about the core mechanics,
the small details we can work around later on.
So with that said, I hope you enjoy it.
I hope you are committing your changes
every single video.
I hope you left me a review
because it helps me out a lot.
And will that out of the way?
In the next video, we are going to be taking
our boss to the second stage,
second or two, the angry stage.