What we will do by now.
We're going to create a circle around this anime game object, and if this player game object get inside
of it, then we will stop patrolling.
Instead, we're gonna attack on this player game object.
So we will do the same thing as we have done with anime one game object.
So let's do that.
But in this situation, we're gonna stop patrolling once our player game object gets in attack range.
So let's do that.
So we will select our enemy game object in the hierarchy.
Let's go over to enemy two script in Inspector, and let's double click on it in order to open that
up.
And now what we're gonna do, uh, we do know that we're gonna use that function called physics 2D.
So let's create a first a variable to just keep track of if this player is attacked inside this attack
range or not.
So we're going to create a variable for it.
So to create a circle around that we do know that we need some value.
So let's create another header.
Let's give it a title.
So we're going to type header.
And we will say something like attack.
Attack.
Sorry this will be attack attack.
And then let's create a variable for this.
So first variable will be just to keep track of if our player game object inside that gate attack range
or not.
So that will be bool parameter.
We're just going to keep track of if the player is attack range or not.
So we're going to make it private.
It doesn't need to be public and that will be bull.
And we can call this that is player is player in attack range.
Something like that is player in attack range.
And let's close that up with semicolon.
And we will go over to this start function.
So in the start of the game what we will do we will set is player attack range equal to false.
Because in the start of the game.
We're not going to be closer to this enemy.
Enemy two game object.
So that's why.
And now let's create the circle.
To create the circle first thing we need a point from where we want to create the circle.
So since we're going to create the circle from center position of our enemy enemy two game object.
So we don't need to define that.
So so we just gonna use transform dot position which is basically means current position of that enemy
two game object.
So we don't need the position.
But however we need the radius of it.
So let's create a serialized field and this will be private float this we will this will be float.
And we can call this one attack range radius.
So let's call it attack range radius.
And then by default I'm going to set it to something like six F or six unit.
And the third argument we need that uh we which game object we really want to damage.
So in our case we're gonna damage our player.
So let's create another field.
Or basically we want to check with player game object.
So that's why.
So we're gonna this will be private layer masks.
And make sure you just mark this as a serialized field.
And we can call this one something like target.
Uh, target layer or --.
Or you can say attack layer.
Uh, but I'm going to call it What is player.
Let's close that up with semicolon.
And now we're going to press down Ctrl S to save our script.
And uh so, so we don't need to move over to let's firstly move over to unity editor and let's assign
those variable.
So so the first thing just going to pop up which will be our attack radius.
And the second thing will be uh what is ground.
Basically the layer marks.
So basically what we want to check with player.
So as a layer mask we will choose our player.
And before you choose your player make sure you assign your player game object as a player layer, as
we have done already.
So what is player?
As you can see it says nothing.
So we're going to choose our player.
So this player the circle just going to check with this player game object only.
So now let's open up our animator script and what we will do uh to create the circle let's move over
to this update function.
So this update function runs every single frame.
So every single frame, uh, we just want to create the circle.
Or we just want to keep track of where player is.
So to create the circle we need to use this physics 2D dot overlap circle.
We do no overlap circle.
And this as a first argument.
It's gonna take the point from where we want to draw.
So we're going to pass our transform dot position.
And transform dot position basically means current position of that animator game object.
Or you can simply say center position of that animator game object.
And the second thing, it's gonna take the radius.
So I'm going to pass my attack range radius.
And the third argument it's going to take the layer mask.
So as a layer mask we will pass what is layer.
And then close that up with semicolon.
So the circle is just going to create from this a center position of this anime two game object with
attack with attack range radius.
And it's just going to check with this player game object, a player game object.
So this as function right over here, it can give us a true or false value depending upon if the circle
is really colliding with this player game object or not.
If it's then this function over here, it's going to give us a true value, else it will give us a false
value.
So what we will do, we're going to store the result or a result into a collider 2D since this is just
going to work as a collision.
So that's why we need to put it in a variable or in a container which will be the container type will
be collided 2D.
Or you can simply say the parameter container and we can call this one call info or short for collision
information.
And let's set equal to this.
So we're storing the result into this call info.
So this call info right over here it can be true or false depending upon what this function is really
doing.
Basically if the circle collides with this player game object then it's gonna be true.
So this call info will be true are true because we're storing the result into this collider 2D call
info parameter.
So this can be this info right over here.
It can be true or false depending upon a circle.
The circle is really colliding with that player game object or not.
So what we will do now if the call info is true.
So we will check.
So let's make first bracket and some curly brackets and what we want to check over here.
We basically want to check if call info is equals equals to true.
Or we can just leave like this.
It also means to means that the circle colliding with this player came object.
So now what we need to do?
We need to move our.
We need to stop patrolling first.
We need to stop patrolling.
And after that we just need to get closer to this player game object.
And once we reach some certain distance, then this enemy two game object need to play the attack animation
clip, uh, attack animation clip that we created, as you can see right over here.
So we forgot to make transition between this animation clip.
So we need to do that.
But we're going to take a look on it later.
So let's uh, go to our script.
And if call info is true basically means player in attack range.
So what I'm gonna do, we just created this parameter.
As you can see right over here it says is player in attack range.
So what we will do, we will set it to true because we uh the circle just colliding with that player
game object.
So that's why we're going to set is player attack range that, uh, the, the variable that we created
to just keep track of if the player in attack range or not.
So we're going to set is player in attack range equal to true if call info is true basically means player.
Just get inside of that circle.
So that's why we're setting this is player attack in attack range to true.
Let's suppose if the circle is not colliding with that player means it's just outside of the circle.
Our player.
So else else condition what we're going to do else if our player game object outside of that circle.
So in this moment we're gonna set is player in attack range to false because it's not inside of that
circle.
So that's why we're going to set is player in attack range equal to false.
Equal to false.
And that's it.
And now uh and now what we're going to do, we're going to press down control s to save our script and
then move over to unity.
So we should able to see the circle, but we not gonna be able to see that, uh, because we what we
basically have to do, uh, we need to draw the circle.
And we do know that for that, for that, we need to use a function calls on draw gizmo selected.
So we will move over to one more time in our script.
And over here we're gonna do, uh, the function we created, which is Ondraw gizmo selected.
So let's do it in the bottom somewhere over here.
To draw the circle, we need to use this gizmo straw where I sphere function.
And we don't need to check if we assign it, uh, the position from where we want to create the circle
since we do one, uh, since we want to draw the circle from center position.
So that's why we don't need to assign a point for it.
So that's why we're going to use directly this, uh, this gizmo dot draw where sphere and this, uh,
this function as a first argument, it's going to take the point.
So since we want from center position so we can simply type transform dot position.
And before we draw the circle we don't need to assign this transform dot position because it's enemy
center position or current position of that enemy to game object.
And the second thing it's going to take the radius.
So I'm going to pass my attack range radius.
And let's close that up with some y colon.
And also I want to choose a different color for this.
So before we draw I'm going to assign a color.
To assign a color we're going to use this gizmo dot color.
And we can set the color that we would like to draw with.
So I'm going to draw with color dot color dot something like green uh green something like that.
And let's close that up with semicolon.
And now I'm going to press Ctrl s on my keyboard in order to save my script.
And now let's head over to unity.
So let's head over to unity and we will able to notice.
So for to see that parameter which is a is player in attack range, we need to move over to debug mode
instead of inspector mode to see that.
As you can see, as soon as our skip just gets compiled, we can see the circle just drawn from this
enemy and enemy to game object center position, or from or from this current position of this enemy
to game object.
I want to make it a little bit less.
Basically, I'm gonna down my attack range radius to something like four, and that's pretty much fine.
So once we're gonna get inside of this circle then that is a is player in attack range parameter is
gonna be true.
Else it's gonna be false.
So to check that we can see underneath our enemy to skip.
We're not able to see our the that bool parameter which is is player in attack range since we just made
it private.
So we want to see to see that.
So we need to move over to Inspector in the top right.
We're going to click on it and we're going to choose this debug mode.
And as soon as we have chosen debug we can see private parameters.
And each player in attack range is right over here.
And it's unchecked basically false.
And if we just go ahead and then click on this play button, you will see as soon as we're going to
get inside of this circle of this enemy two game object, this is player in attack range is going to
be true basically means check.
And once we are outside of it, it's gonna be false.
So let's get inside.
As you can see.
Indeed.
But once we get outside of it, it's get unchecked.
But once we get in, we can see it's checked.
Basically just setting it to true or false, or you can simply say check or uncheck.
So we will use this parameter to attack or to stop patrolling.
So let's get out of play mode and let's move over to our Inspector mode.
To do so, let's go over to Inspector one more time.
In the top right.
We're going to click one more time and let's click on this normal in order to go back.
So we will use that parameter which is is player in attack range.
So we will open up our animator script by double clicking on it.
And we're gonna uh so over here as you can notice that we patrolling right over here.
So let's put this, uh, all of this code inside a function.
So let's create a function.
Uh, some we're going to call that function to something like patrolling.
So let's uh for function we're going to type void.
And we have to name the function.
So let's call this function like patrolling or patrol enemy.
Let's call it patrol enemy.
Let's make first bucket and some curly brackets.
And what we want to do, we're going to put all of this line of code.
So this line of code right over here.
As you can see it's moving our uh enemy to game object.
And it's creating the reef in the down direction and then it just flipping itself.
If it's at the at the end of the crown or not.
So this line of code right over here is for trolling.
So when I cut this line of code and we're going to put it inside this petrel enemy function to make
it even simpler or to readable, you can simply say, so we're going to select all of this and we're
going to press down Ctrl X for cut.
And we're going to paste inside this petrel enemy function by Ctrl V.
And now it's done as you can see.
So this seems a lot more cool clean.
And what we have to do now we basically need to call this petrel enemy function.
And this function is just going to patrol our enemy.
But when we need to call it once our uh is player in attack range is true basically means player.
Uh, if uh sorry.
If uh is is player in attack range is false, then we need to call it.
Because if the circle if this if the player inside of this circle, then this enemy two game object
need to stop patrolling.
But if the player game object is outside of this circle, then this enemy two game object need to patrol
around this ground.
So we're going to call this patrol enemy function once our player game object is outside of our circle.
So we're going to check.
So we will use this if condition inside our update function.
Then make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check if is is player a player in attack range.
If it's equals equals to false basically means player is outside of this, of this circle or of this
attack range of this enemy to game object.
Then.
Then what this enemy two game object need to do.
It's need to patrol.
So that's why if it's attack range is player in attack range equals equals to false basically means
player is outside of that circle.
Now what, uh, what, uh, that anime need to anime to game object need to do.
It's need to patrol.
So that's why we're gonna call the function over here, which is patrol enemy.
And we do know this function just going to do these things.
Basically, it's gonna patrol.
Let's suppose if we are attack range.
So we're going to use this else condition.
Else if our player inside of that basically uh, this uh, circle that we created from this enemy two
game object inside of this circle, then this enemy two game object need to stop patrolling.
Uh, so that's why we didn't call inside this else function, which is patrol enemy.
So for now, we're going to print out a message.
So let's type this debug.log in order to print out our message, uh, let's say something like an,
uh, sorry, we will say something like player, player in attack range.
Player in attack range.
Something like that.
Uh, else a else.
Our Is player in in attack range is, uh, true basically means player in inside of this screen uh,
circle that we created from this anime to game object.
Then what we are doing, we simply printing out this message says player in attack range.
Once our Is player in attack range is true, as you can see.
And we do know that if a player in attack range is true true, then we just printing out this message
says player in attack range.
But if the player is outside of that circle, then what we doing as you can see, is player in attack
range.
If it's equals equals to false basically means player game object outside of that circle.
Then we calling the enemy function.
And this function basically patrolling our enemy.
And it's flipping itself according to where a where our enemy to game object is.
So I hope you get the idea.
And now we're going to press down Ctrl S to save our script.
And now let's move over to unity.
You will see as soon as when I get inside of this, uh, attack range.
Radius circle, then this, uh, anime two game object.
Just gonna stop moving.
Stop moving.
And that message just gonna pop up.
But if we not in, then it's, uh, basically our anime two game object just gonna patrol around this
ground.
So let's check that.
So I'm going to select my console tab, and let's click on this play button in the top in order to check
that out.
And we can make it a little bit bigger.
This you can simply say, uh, simply say this circle that we created from this anime to game object.
We can make it bigger.
But before we do, let's have a look.
And now we can see as soon as this player game object collided with this circle that we created from
this anime to game object, this anime two stops moving.
And if we move over to our console tab, we can see a message just getting pop up every single frame
says player in attack range.
So what it's need to do?
Firstly, it's need to get closer to this player game object than it's need to attack.
But once we get outside of this circle, we can see enemy is just patrolling around this ground and
the message just gets stopped popping out.
But once we get in, we can see this player stop.
Sorry, this enemy two game object stops moving and the message just getting pop up says player in attack
range.
So I hope you get the idea that how you can do it and I'm gonna get out of play mode.
And instead of playing the that animation clip, which is the work one we need to attack on this player
game object, uh, so we can just increase our circle.
So I'm gonna increase that.
So let's go over to our enemy two script.
Uh, then, uh, enemy anime to script an attack.
Sorry we didn't given a title.
If I open that up and then attack.
Okay, so we're gonna put down this, uh, ace player attack range.
Something over here.
Attack checking like that.
Let's do it one more time.
Attack checking.
And let's press down Ctrl s to save our script.
And now let's move over to unity.
So the title just gonna pop up underneath our this variable, which is, uh, attack radius, attack
range radius.
So just wait a moment and we will see.
See that?
So now we can see attack checking title just gets pop up.
And I want to bump up the value of this attack range radius.
something around eight, and this seems pretty much cool.
As you can see.
And I will move over to overrides in the inspector.
Then apply all and we can check this out as well.
So I'm going to select my player game object.
And let's drag it a little bit further away from this animated game object for checking purposes.
So I'm going to move it somewhere around here.
So as soon as the circle just gonna collide with this player game object, then this animate just gonna
stop moving.
Because if we move over to our script, as you can see what we're doing, firstly we checking if is
player in attack range is equals equals to false basically means this player is outside of that circle.
So what we're doing in that situation we're just calling the patrolling patrol enemy function.
And this function basically doing these things, basically patrolling our enemy.
But once we get inside of that attack range radius, then as you can see, we printed printing out a
message says player in attack range basically means this player just collided with this enemy to anime
to game object circle, and it's going to stop moving.
We only printing out the message in the console tab as you can notice.
So let's move over to unity and let's click on this play button in the top in order to play our game.
And let's have a look.
So as soon as we're gonna collide with this circle of this enemy two game object, then this enemy two
game object just gonna stops moving and the message just gonna pop up.
As you can see, indeed, this enemy two game object just gets stops and the message just getting pop
up in inside our console tab, we can see player in attack range.
So I hope you get the idea.
So I'm gonna get out of play mode.
And we need to make transition between this animation clip that we created inside this enemy to game
object.
So we need to do that as well.