What we need to do.
We need to chase our player game object.
So if I just go ahead and then click on this play button, you will see as soon as we collide with the
circle enemy just gonna stops.
So so what we want to do, we want to chase our player game object A with little bit high speed.
So let's have a look.
So if I get closer to it as you can see enemy stops moving.
And if I move over to my console tab we can see a message just pop up says player in attack range instead
of printing out message.
Firstly we just want to get closer to this enemy.
Sorry player game object.
So we're gonna get a get closer to some certain distance, uh, from this uh, from our enemy game object.
So let's get out of play mode and let's do that.
So what we will do, we will open up this, uh, message by double click on it in our console tab.
As you can see, we just printing out message, uh, message right over here says a player in attack
range.
So what we want to do right over here, instead of printing out message, we want to chase our player
game object.
So what we can simply do, we can go to top of our class and we can make a variable for our speed.
So let's go to top of our animator script and let's create a serialized field.
So let's create a serialized field private.
And this will be float.
And we can call this one something like Chase speed.
So let's call it Chase speed.
And by default I'm gonna give it something like four f.
So we have given work speed to two.
So that's why I'm just giving speed to four.
As you can see right over here.
Because we're gonna chase our player game object a little bit with a little bit high speed.
So that is the reason.
So now let's move over to this else condition.
So else if we are in attack range.
Attack range if our player in attack range now, what we're going to do, we're going to set the position.
So transform dot position and transform dot position over here basically means current position of that
enemy to game object.
And we're going to set the position equal to vector two vector two dot move towards function.
And this function basically it's going to move.
So first thing it's going to take the current position where you are where you right now.
And the second thing it's going to take the target position where you want to go.
And the third argument it's going to take the speed with how much speed.
You just want to move over to that point.
So what we want to do as a first argument, we're going to type this transform dot position, which
is current position of our enemy to game object.
Second thing it's going to take the target position.
So the target position will be our player position.
So that's why we're going to type this player dot position.
And the third argument it's going to take the speed.
So we will pass our chase speed and make sure you multiply with that time.
Dot delta time to make it frame rate independence and then close that off with semicolon.
And if we save our script and move over to unity, you will see as soon as we're gonna collide with
that circle, this enemy two GameObject just gonna chase our player GameObject with four unit of speed
as we specified.
So this Transform.position.
Basically, we're setting the current position of that enemy to GameObject equal to this as you can
notice.
So now it should be work fine.
So let's have a look on it.
So as soon as we collide with that circle enemy enemy two GameObject.
Just gonna chase our player GameObject with our Chase speed Chase speed amount.
So just wait a moment to compile our script and once the compilation is done, we can check that out.
So just wait a little moment and just wait a moment.
And we also need to stop, uh, moving our enemy to game object once it gets some certain distance.
Uh, to our player game object, we need to stop doing that.
But before we do, let's, uh, let me show you.
So it's going to take a few seconds to compile our script.
I really apologize, it's just taking a little bit longer time since I have slow system.
So now the compilation is done and I'm going to select my player game object and let's move it somewhere
over here by selecting this move tool or this tool in the scene view.
And let's check that out.
And if I just go ahead and then click on this play button, you will see this uh animated game object
just going to chase our player, uh, game object with little bit faster.
And we can see underneath our enemy to skip the chase speed.
By default we have given four, and now we can see as soon as we colliding with the circle, we can
see, uh, by now that, uh, this, uh, enemy two game object just chasing our player, but the problem
is that it's just pushing our player.
So we need to stop doing that.
Basically, we need to stop moving our this enemy to game object.
Once it's get a some certain distance with our player game object.
So we need to stop doing that.
So let's get out of play mode for it.
We will go over to script.
So before we chase our player game object we just want to check the distance between them.
So if it's some certain distance then this enemy need to stop chasing.
And then this enemy to game object need to play the attack animation clip.
So we're going to do that.
So before we just we just want to check the distance between them.
So we're going to create a retrieve distance parameter which is basically the distance we want to check
between our this player game object and this enemy game object.
So let's go to the top of our class and let's create another variable.
And we can make it over here in attack check in title.
So let's make it here.
So we're going to make serialized field and that will be Private Float.
And we can call this one something like retrieve distance.
So I'm going to call it retrieve retrieve distance.
And if I misspelling it I really apologize for it.
And by default I'm going to say some set something like 2.5 F.
And let's close that up with semicolon.
So this will be our retrieve distance.
So now let's move over to else condition.
So before we chase our player game object we're going to check the distance between our player and enemy
to game object.
So we're going to use this if condition.
Let's make first bracket and some curly brackets.
So what we are going to do over here.
We're going to check the distance between our player and the enemy.
To check we're going to use this vector to dot distance distance function.
And this vector two dot distance function.
Just going to take two arguments two arguments basically two positions.
And and it's going to compare between them.
So first position I'm going to pass transform dot position which is basically means to a position enemy
to current position.
And the second position we want to check with our player position.
So we're going to type this player dot position.
And if the this and now if the distance between our enemy two and player position if it's greater than
our retrieve distance if that is the case basically means that our this enemy two, this enemy two and
this player game object either the distance between them if it's greater than retrieve distance means
this player and this this enemy two game object still not yet closer to this player game object.
So what we need to do, what we need to do in that moment.
We need to chase our player.
So that's why we're going to put this line of code inside this if condition.
Because over here we're checking taking our enemy in our player game object?
If the distance between is still greater than retrieved, distance means this.
This enemy two game object is still not a get closer or not something like over here.
Uh, so then what we're gonna do, we're going to chase.
So that's why we will put this line of code by control X, and let's put it over here by control V.
So we're checking if the distance between our enemy and this player, if it's greater than retrieve
distance means our enemy two game object is still not, uh, too much closer to our player game object
then what we are doing, we chasing.
But let's suppose the distance between our player, this player game object, and this enemy two game
object is now lesser than retrieved.
Distance means it just gets too closer.
So now what we have to do, we basically need to stop, uh, moving.
So that's why in the else condition so else if our distance between our player and that enemy two game
object is a it's lesser than retrieve distance means it's now close to our player game object.
So it's what it's need to do.
This enemy two game object need to attack on this player game object because it's now closer to this
player game object.
So for now we're gonna print out a message.
So for that we're going to type Debug.log for printing out message.
Say let's say something like play attack animation play.
Let's say something like play enemy enemy to attack attack animation and attack animation.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save our script.
So firstly what we're doing we just using this else condition.
Else if our player player game object just get in attack range then what we're doing we just moving.
But before we are moving our enemy to to game object or basically chasing our player, we We're just
checking the distance between them.
If it is still greater than retrieve distance means it's not a.
Our game object is still not get too much closer to player game object than we're chasing.
And if it's too much close by now, then we're just printing out this message says play an enemy to
Attack animation clip.
Uh, sorry.
Uh, we play enemy to attack.
Animation message.
Just gonna pop up.
If the distance between our player and this enemy to game object is now too much close, or it's less
than basically that retrieve distance and we can notice underneath our enemy to script.
So now let's take that out.
So if I just go add and then click on this play button in the top in order to play our uh, our game,
you will see if uh, firstly our enemy to game object.
Just gonna chase our player game object.
But once it's just gonna get too much close basically, as you can see.
So over here, if the distance, uh, basically the distance between our enemy two and this player is
now lesser than retrieve distance a retrieve distance.
And in that moment, what we are doing, we just simply printing out this message.
As you can see, play enemy to attack animation instead of chasing.
As you can see as basically our play, this enemy just stops moving once it's get too much close, uh,
close or the retrieve distance is now lesser than between our this enemy two game object and this player
game object.
I want to increase the retrieve distance parameter.
So I want to stop my enemy somewhere over here a little bit further away from this player game object.
So that's why we can just increase the retrieve distance parameter parameter a little bit up.
So I'm going to get out of play mode and retrieve distance.
I'm going to choose something around four and change speed.
I'm going to do something like 2.5.
And the speed should be 1.2.
I just want to move a little bit and I'm going to select my player game object.
And let's move it over here somewhere here.
And let's click on this play button in the top in order to play our game.
So you will see as soon as this player game object.
Sorry, sorry.
As soon as this animator game object gets closer to this player game object, it's just gonna stop if
the distance between is greater than retrieve.
As you can see now, the now our animator game object stops moving because the distance between our
player and this animator game object is lesser than that retrieve distance.
But if the distance is greater than retrieve distance, basically the distance between our enemy to
and this player, if it's still greater than retrieve distance, then we just chasing our player.
But if the distance between our enemy to and the player if it's lesser than, lesser than than what
we're doing in else condition else the distance between our this, uh, this player game object and
this enemy two game object is now less than retrieved distance.
Then we simply printing out this message in the console tab saying like playing enemy to attack animation.
So I hope you get the idea that how you can do it.
But we have a problem.
If we jump over, we can see this enemy two game object, uh, trying to jump as well.
So we need to stop doing.
We want to, uh, chase our player game object only in the x axis.
Or you can simply say in the horizontally.
We don't want this enemy to game object to chase our player game object vertically or in the y axis.
So what we're going to do we for that we're going to do the scripting.
So let's get out of play mode.
So we only want to chase our player game object in the x axis.
Or you can simply say in the horizontally.
So for that what we need to do we basically need to replace this player dot position, basically the
target position where we want to go.
So we need to replace this.
So how we can replace this.
For that we're going to create a vector two parameter.
And we're going to call that parameter to something like a target.
So let's create a vector two parameter.
And we can call this one to something like target or target position.
And we're going to set equal to a new vector two.
And this vector two is just going to have x axis and y axis.
So we only want to follow our player this player game object in the x axis or horizontally.
So that's why we're going to replace this x with our player.
So we will go to player dot position dot x.
Basically we just accessing the player position.
And in the in the y we don't want to follow our player game object at all in the vertically.
Or you can simply say in the y axis.
So that's why in the y what we're going to do, we're going to pass transform dot position dot y.
It is basically means that current position of that enemy to game object itself in the y axis.
So this target post.
So basically this target post vector just gonna follow our player in the x axis only.
As you can see we just replace x with our player dot position.
Dot x means we're just gonna follow our player in the x axis only.
And we're not doing at all in the y axis.
So we just pass transform dot position.
Dot y basically means that current position of this enemy to game object in the y axis.
So this enemy two game object not gonna follow our player in the y axis.
And now what we have to do, we need to replace this player dot position with this target pose.
And this target pose.
Vector is just gonna follow our player in the x axis only.
And it's not gonna do in the y axis.
So we're gonna replace this player dot position basically the target pose.
So this vector2 dot move towards function as a second argument.
It's gonna take the target pose.
So we will pass our this pose which is our target post.
So we're going to pass our target post.
And we do know that this target post parameter, this vector to target post parameter.
Just going to follow our player.
Basically this player GameObject in the x axis only.
And now we're going to press down Ctrl S in order to save our script.
And now let's move over to unity.
And by now we're not gonna be able to see or not gonna able to notice that thing.
Basically, our enemy two GameObject not gonna follow our player GameObject in the Y axis or vertically.
It's gonna follow in the x axis or horizontally.
So that's why we just created that factor two parameter to just follow our player, uh, GameObject
in the x axis only.
And now if we go ahead and then hit this play button in the top, you will see this play this enemy
two game object.
By now it's gonna follow our player GameObject in the horizontally or in the x axis only.
So let's have a look on this Is and we're going to jump over it.
And we can see by now this enemy two game object is not jumping in.
Basically, it's not moving in the y axis or vertically.
And when once we get in attack basically closer to this enemy to game object or this enemy to game object,
get closer to this player game object.
As you can see, we just printing out a message in our console tab says play a play enemy to attack
animation clip.
But once we get a little bit away from this enemy two game object, we can see it's now changing because
the position is greater than retrieved distance.
But the position between our player and this enemy to if it's lesser than retrieved distance than what
we're doing.
So in that moment we just printing out the message in the console says play enemy to attack animation.
Basically, we can simply assume that the distance is too much close.
So that's why we just printing out this message instead of chasing our player game object, but the
distance between our this enemy to and this player, if it's greater than retrieve distance basically
means sorry and basically means the basically means that our player and this enemy to basically little
bit further away.
So in that moment, if I just cut this and then move over to my enemy to script what we can, what we're
doing in that moment, we're just chasing our player game object in the x axis only, as you can see,
if the distance between our enemy and this player game object, if it's greater than retrieve distance,
basically means the distance between our player and this enemy to is not too much close yet.
So that's why we chasing our player game object.
But if it's too much close, basically the the distance between is now lesser than this retrieve distance
so else condition.
If the if our enemy to game object and the player game object is still to, uh, basically, uh, too
much close.
Then we're just printing out this message in the console, says play Enemy to attack animation message
in our console tab.
So I hope you get the idea that what we are just doing and what we have to do, uh, we now need to
play the attack animation clip of this enemy two game object.
Once it's get in attack range, and if the distance between is now too much close, then this enemy
two game object need to play the attack to animation clip instead of playing the walking animation clip
over again and again.
So I'm gonna get out of play mode, and I hope you get the idea that how you can do it and make sure
you select your enemy two game object and this player then go over to Inspector Overrides and then apply
all.
So if you change something around inside this enemy two script or anything or with any component, so
the changes can just gonna be saved into this enemy two GameObject prefab as well.