WEBVTT

00:00.000 --> 00:00.920
What we need to do.

00:00.960 --> 00:03.560
We need to chase our player game object.

00:03.560 --> 00:09.040
So if I just go ahead and then click on this play button, you will see as soon as we collide with the

00:09.040 --> 00:11.360
circle enemy just gonna stops.

00:11.360 --> 00:18.200
So so what we want to do, we want to chase our player game object A with little bit high speed.

00:18.440 --> 00:19.480
So let's have a look.

00:19.720 --> 00:23.960
So if I get closer to it as you can see enemy stops moving.

00:24.080 --> 00:31.160
And if I move over to my console tab we can see a message just pop up says player in attack range instead

00:31.160 --> 00:32.760
of printing out message.

00:32.800 --> 00:35.680
Firstly we just want to get closer to this enemy.

00:35.920 --> 00:37.440
Sorry player game object.

00:37.600 --> 00:46.040
So we're gonna get a get closer to some certain distance, uh, from this uh, from our enemy game object.

00:46.200 --> 00:48.960
So let's get out of play mode and let's do that.

00:48.960 --> 00:54.440
So what we will do, we will open up this, uh, message by double click on it in our console tab.

00:54.600 --> 01:02.680
As you can see, we just printing out message, uh, message right over here says a player in attack

01:02.680 --> 01:03.320
range.

01:03.320 --> 01:09.280
So what we want to do right over here, instead of printing out message, we want to chase our player

01:09.320 --> 01:10.120
game object.

01:10.120 --> 01:17.280
So what we can simply do, we can go to top of our class and we can make a variable for our speed.

01:17.280 --> 01:22.120
So let's go to top of our animator script and let's create a serialized field.

01:22.400 --> 01:26.240
So let's create a serialized field private.

01:26.560 --> 01:28.520
And this will be float.

01:28.520 --> 01:31.440
And we can call this one something like Chase speed.

01:31.480 --> 01:33.280
So let's call it Chase speed.

01:33.280 --> 01:37.200
And by default I'm gonna give it something like four f.

01:37.200 --> 01:39.840
So we have given work speed to two.

01:40.160 --> 01:43.320
So that's why I'm just giving speed to four.

01:43.360 --> 01:45.640
As you can see right over here.

01:45.640 --> 01:51.360
Because we're gonna chase our player game object a little bit with a little bit high speed.

01:51.360 --> 01:52.880
So that is the reason.

01:52.880 --> 01:55.560
So now let's move over to this else condition.

01:55.560 --> 01:59.000
So else if we are in attack range.

01:59.240 --> 02:04.750
Attack range if our player in attack range now, what we're going to do, we're going to set the position.

02:04.750 --> 02:10.990
So transform dot position and transform dot position over here basically means current position of that

02:10.990 --> 02:12.430
enemy to game object.

02:12.430 --> 02:18.750
And we're going to set the position equal to vector two vector two dot move towards function.

02:18.750 --> 02:21.350
And this function basically it's going to move.

02:21.350 --> 02:27.230
So first thing it's going to take the current position where you are where you right now.

02:27.230 --> 02:31.110
And the second thing it's going to take the target position where you want to go.

02:31.230 --> 02:34.710
And the third argument it's going to take the speed with how much speed.

02:34.750 --> 02:37.350
You just want to move over to that point.

02:37.350 --> 02:42.750
So what we want to do as a first argument, we're going to type this transform dot position, which

02:42.750 --> 02:46.550
is current position of our enemy to game object.

02:46.550 --> 02:49.070
Second thing it's going to take the target position.

02:49.070 --> 02:52.310
So the target position will be our player position.

02:52.310 --> 02:55.310
So that's why we're going to type this player dot position.

02:55.550 --> 02:58.310
And the third argument it's going to take the speed.

02:58.310 --> 03:02.510
So we will pass our chase speed and make sure you multiply with that time.

03:02.550 --> 03:08.150
Dot delta time to make it frame rate independence and then close that off with semicolon.

03:08.150 --> 03:14.030
And if we save our script and move over to unity, you will see as soon as we're gonna collide with

03:14.030 --> 03:20.590
that circle, this enemy two GameObject just gonna chase our player GameObject with four unit of speed

03:20.590 --> 03:21.870
as we specified.

03:22.070 --> 03:23.670
So this Transform.position.

03:23.670 --> 03:29.830
Basically, we're setting the current position of that enemy to GameObject equal to this as you can

03:29.830 --> 03:30.310
notice.

03:30.590 --> 03:32.830
So now it should be work fine.

03:32.830 --> 03:34.270
So let's have a look on it.

03:34.590 --> 03:39.390
So as soon as we collide with that circle enemy enemy two GameObject.

03:39.390 --> 03:46.030
Just gonna chase our player GameObject with our Chase speed Chase speed amount.

03:46.030 --> 03:52.990
So just wait a moment to compile our script and once the compilation is done, we can check that out.

03:52.990 --> 03:57.790
So just wait a little moment and just wait a moment.

03:57.790 --> 04:04.790
And we also need to stop, uh, moving our enemy to game object once it gets some certain distance.

04:05.070 --> 04:09.270
Uh, to our player game object, we need to stop doing that.

04:09.270 --> 04:11.990
But before we do, let's, uh, let me show you.

04:11.990 --> 04:15.350
So it's going to take a few seconds to compile our script.

04:15.710 --> 04:21.550
I really apologize, it's just taking a little bit longer time since I have slow system.

04:21.550 --> 04:27.510
So now the compilation is done and I'm going to select my player game object and let's move it somewhere

04:27.510 --> 04:32.150
over here by selecting this move tool or this tool in the scene view.

04:32.470 --> 04:34.630
And let's check that out.

04:34.790 --> 04:40.590
And if I just go ahead and then click on this play button, you will see this uh animated game object

04:40.590 --> 04:45.270
just going to chase our player, uh, game object with little bit faster.

04:45.270 --> 04:48.630
And we can see underneath our enemy to skip the chase speed.

04:48.670 --> 04:54.150
By default we have given four, and now we can see as soon as we colliding with the circle, we can

04:54.150 --> 05:00.870
see, uh, by now that, uh, this, uh, enemy two game object just chasing our player, but the problem

05:00.870 --> 05:03.950
is that it's just pushing our player.

05:03.950 --> 05:05.670
So we need to stop doing that.

05:05.670 --> 05:10.230
Basically, we need to stop moving our this enemy to game object.

05:10.230 --> 05:15.950
Once it's get a some certain distance with our player game object.

05:15.950 --> 05:18.030
So we need to stop doing that.

05:18.030 --> 05:19.790
So let's get out of play mode for it.

05:19.830 --> 05:21.750
We will go over to script.

05:21.750 --> 05:27.310
So before we chase our player game object we just want to check the distance between them.

05:27.550 --> 05:32.630
So if it's some certain distance then this enemy need to stop chasing.

05:32.630 --> 05:36.950
And then this enemy to game object need to play the attack animation clip.

05:36.950 --> 05:38.070
So we're going to do that.

05:38.070 --> 05:41.630
So before we just we just want to check the distance between them.

05:41.950 --> 05:48.750
So we're going to create a retrieve distance parameter which is basically the distance we want to check

05:48.790 --> 05:53.230
between our this player game object and this enemy game object.

05:53.230 --> 05:57.430
So let's go to the top of our class and let's create another variable.

05:58.230 --> 06:02.180
And we can make it over here in attack check in title.

06:02.180 --> 06:03.660
So let's make it here.

06:03.860 --> 06:09.060
So we're going to make serialized field and that will be Private Float.

06:09.060 --> 06:12.380
And we can call this one something like retrieve distance.

06:12.380 --> 06:16.420
So I'm going to call it retrieve retrieve distance.

06:16.540 --> 06:20.380
And if I misspelling it I really apologize for it.

06:20.380 --> 06:24.700
And by default I'm going to say some set something like 2.5 F.

06:24.700 --> 06:26.860
And let's close that up with semicolon.

06:27.060 --> 06:29.540
So this will be our retrieve distance.

06:29.540 --> 06:31.860
So now let's move over to else condition.

06:31.860 --> 06:38.060
So before we chase our player game object we're going to check the distance between our player and enemy

06:38.060 --> 06:38.980
to game object.

06:38.980 --> 06:40.580
So we're going to use this if condition.

06:40.580 --> 06:43.220
Let's make first bracket and some curly brackets.

06:43.220 --> 06:45.100
So what we are going to do over here.

06:45.100 --> 06:48.660
We're going to check the distance between our player and the enemy.

06:48.700 --> 06:53.340
To check we're going to use this vector to dot distance distance function.

06:53.340 --> 06:55.740
And this vector two dot distance function.

06:55.740 --> 07:00.780
Just going to take two arguments two arguments basically two positions.

07:00.780 --> 07:03.780
And and it's going to compare between them.

07:03.780 --> 07:10.780
So first position I'm going to pass transform dot position which is basically means to a position enemy

07:10.780 --> 07:12.220
to current position.

07:12.220 --> 07:16.540
And the second position we want to check with our player position.

07:16.540 --> 07:18.980
So we're going to type this player dot position.

07:19.180 --> 07:26.220
And if the this and now if the distance between our enemy two and player position if it's greater than

07:26.220 --> 07:33.180
our retrieve distance if that is the case basically means that our this enemy two, this enemy two and

07:33.180 --> 07:40.020
this player game object either the distance between them if it's greater than retrieve distance means

07:40.060 --> 07:46.740
this player and this this enemy two game object still not yet closer to this player game object.

07:46.740 --> 07:50.660
So what we need to do, what we need to do in that moment.

07:50.660 --> 07:52.340
We need to chase our player.

07:52.340 --> 07:57.180
So that's why we're going to put this line of code inside this if condition.

07:57.180 --> 08:02.100
Because over here we're checking taking our enemy in our player game object?

08:02.140 --> 08:07.140
If the distance between is still greater than retrieved, distance means this.

08:07.300 --> 08:14.220
This enemy two game object is still not a get closer or not something like over here.

08:14.500 --> 08:17.740
Uh, so then what we're gonna do, we're going to chase.

08:17.740 --> 08:23.980
So that's why we will put this line of code by control X, and let's put it over here by control V.

08:24.140 --> 08:29.620
So we're checking if the distance between our enemy and this player, if it's greater than retrieve

08:29.620 --> 08:36.500
distance means our enemy two game object is still not, uh, too much closer to our player game object

08:36.540 --> 08:38.420
then what we are doing, we chasing.

08:38.420 --> 08:44.540
But let's suppose the distance between our player, this player game object, and this enemy two game

08:44.580 --> 08:46.660
object is now lesser than retrieved.

08:46.660 --> 08:49.940
Distance means it just gets too closer.

08:49.940 --> 08:54.580
So now what we have to do, we basically need to stop, uh, moving.

08:54.580 --> 09:01.850
So that's why in the else condition so else if our distance between our player and that enemy two game

09:01.890 --> 09:08.250
object is a it's lesser than retrieve distance means it's now close to our player game object.

09:08.250 --> 09:10.370
So it's what it's need to do.

09:10.410 --> 09:16.170
This enemy two game object need to attack on this player game object because it's now closer to this

09:16.170 --> 09:17.530
player game object.

09:17.530 --> 09:19.930
So for now we're gonna print out a message.

09:19.930 --> 09:23.330
So for that we're going to type Debug.log for printing out message.

09:23.370 --> 09:27.130
Say let's say something like play attack animation play.

09:27.330 --> 09:36.610
Let's say something like play enemy enemy to attack attack animation and attack animation.

09:36.610 --> 09:38.770
And let's close that up with semicolon.

09:38.770 --> 09:41.730
And we're going to press down Ctrl S to save our script.

09:41.930 --> 09:45.570
So firstly what we're doing we just using this else condition.

09:45.610 --> 09:52.610
Else if our player player game object just get in attack range then what we're doing we just moving.

09:52.610 --> 09:58.690
But before we are moving our enemy to to game object or basically chasing our player, we We're just

09:58.690 --> 10:00.490
checking the distance between them.

10:00.530 --> 10:04.890
If it is still greater than retrieve distance means it's not a.

10:05.130 --> 10:11.370
Our game object is still not get too much closer to player game object than we're chasing.

10:11.370 --> 10:17.490
And if it's too much close by now, then we're just printing out this message says play an enemy to

10:17.530 --> 10:18.930
Attack animation clip.

10:18.930 --> 10:19.570
Uh, sorry.

10:19.850 --> 10:22.090
Uh, we play enemy to attack.

10:22.090 --> 10:23.330
Animation message.

10:23.490 --> 10:24.410
Just gonna pop up.

10:24.410 --> 10:30.730
If the distance between our player and this enemy to game object is now too much close, or it's less

10:30.730 --> 10:36.050
than basically that retrieve distance and we can notice underneath our enemy to script.

10:36.050 --> 10:37.570
So now let's take that out.

10:37.570 --> 10:44.170
So if I just go add and then click on this play button in the top in order to play our uh, our game,

10:44.170 --> 10:47.810
you will see if uh, firstly our enemy to game object.

10:47.810 --> 10:50.650
Just gonna chase our player game object.

10:50.650 --> 10:55.130
But once it's just gonna get too much close basically, as you can see.

10:55.130 --> 11:01.490
So over here, if the distance, uh, basically the distance between our enemy two and this player is

11:01.490 --> 11:05.050
now lesser than retrieve distance a retrieve distance.

11:05.050 --> 11:08.970
And in that moment, what we are doing, we just simply printing out this message.

11:08.970 --> 11:13.450
As you can see, play enemy to attack animation instead of chasing.

11:13.490 --> 11:20.930
As you can see as basically our play, this enemy just stops moving once it's get too much close, uh,

11:21.170 --> 11:27.850
close or the retrieve distance is now lesser than between our this enemy two game object and this player

11:27.890 --> 11:28.770
game object.

11:28.770 --> 11:31.770
I want to increase the retrieve distance parameter.

11:31.930 --> 11:39.650
So I want to stop my enemy somewhere over here a little bit further away from this player game object.

11:39.650 --> 11:45.730
So that's why we can just increase the retrieve distance parameter parameter a little bit up.

11:45.730 --> 11:48.570
So I'm going to get out of play mode and retrieve distance.

11:48.610 --> 11:52.210
I'm going to choose something around four and change speed.

11:52.210 --> 11:54.770
I'm going to do something like 2.5.

11:55.610 --> 11:57.890
And the speed should be 1.2.

11:58.120 --> 12:02.400
I just want to move a little bit and I'm going to select my player game object.

12:02.400 --> 12:05.360
And let's move it over here somewhere here.

12:05.360 --> 12:09.360
And let's click on this play button in the top in order to play our game.

12:09.360 --> 12:12.400
So you will see as soon as this player game object.

12:12.560 --> 12:13.800
Sorry, sorry.

12:13.960 --> 12:20.560
As soon as this animator game object gets closer to this player game object, it's just gonna stop if

12:20.560 --> 12:23.280
the distance between is greater than retrieve.

12:23.400 --> 12:30.320
As you can see now, the now our animator game object stops moving because the distance between our

12:30.320 --> 12:35.200
player and this animator game object is lesser than that retrieve distance.

12:35.200 --> 12:42.120
But if the distance is greater than retrieve distance, basically the distance between our enemy to

12:42.160 --> 12:48.000
and this player, if it's still greater than retrieve distance, then we just chasing our player.

12:48.000 --> 12:54.240
But if the distance between our enemy to and the player if it's lesser than, lesser than than what

12:54.240 --> 13:00.600
we're doing in else condition else the distance between our this, uh, this player game object and

13:00.600 --> 13:04.360
this enemy two game object is now less than retrieved distance.

13:04.360 --> 13:10.600
Then we simply printing out this message in the console tab saying like playing enemy to attack animation.

13:11.040 --> 13:13.680
So I hope you get the idea that how you can do it.

13:13.680 --> 13:15.240
But we have a problem.

13:15.240 --> 13:22.240
If we jump over, we can see this enemy two game object, uh, trying to jump as well.

13:22.240 --> 13:23.920
So we need to stop doing.

13:23.960 --> 13:28.520
We want to, uh, chase our player game object only in the x axis.

13:28.520 --> 13:31.400
Or you can simply say in the horizontally.

13:31.440 --> 13:38.240
We don't want this enemy to game object to chase our player game object vertically or in the y axis.

13:38.240 --> 13:41.720
So what we're going to do we for that we're going to do the scripting.

13:41.720 --> 13:43.360
So let's get out of play mode.

13:43.360 --> 13:47.680
So we only want to chase our player game object in the x axis.

13:47.680 --> 13:50.440
Or you can simply say in the horizontally.

13:50.440 --> 13:55.880
So for that what we need to do we basically need to replace this player dot position, basically the

13:55.880 --> 13:58.160
target position where we want to go.

13:58.160 --> 13:59.960
So we need to replace this.

13:59.960 --> 14:01.680
So how we can replace this.

14:01.720 --> 14:04.440
For that we're going to create a vector two parameter.

14:04.440 --> 14:07.880
And we're going to call that parameter to something like a target.

14:07.880 --> 14:10.000
So let's create a vector two parameter.

14:10.000 --> 14:14.920
And we can call this one to something like target or target position.

14:14.920 --> 14:17.960
And we're going to set equal to a new vector two.

14:18.160 --> 14:22.400
And this vector two is just going to have x axis and y axis.

14:22.640 --> 14:28.760
So we only want to follow our player this player game object in the x axis or horizontally.

14:28.760 --> 14:32.120
So that's why we're going to replace this x with our player.

14:32.120 --> 14:35.600
So we will go to player dot position dot x.

14:35.640 --> 14:38.400
Basically we just accessing the player position.

14:38.600 --> 14:44.720
And in the in the y we don't want to follow our player game object at all in the vertically.

14:44.720 --> 14:47.280
Or you can simply say in the y axis.

14:47.280 --> 14:52.680
So that's why in the y what we're going to do, we're going to pass transform dot position dot y.

14:52.880 --> 14:59.160
It is basically means that current position of that enemy to game object itself in the y axis.

14:59.160 --> 15:00.560
So this target post.

15:00.560 --> 15:06.520
So basically this target post vector just gonna follow our player in the x axis only.

15:06.520 --> 15:10.840
As you can see we just replace x with our player dot position.

15:10.840 --> 15:15.440
Dot x means we're just gonna follow our player in the x axis only.

15:15.440 --> 15:18.320
And we're not doing at all in the y axis.

15:18.320 --> 15:20.520
So we just pass transform dot position.

15:20.520 --> 15:26.360
Dot y basically means that current position of this enemy to game object in the y axis.

15:26.360 --> 15:31.440
So this enemy two game object not gonna follow our player in the y axis.

15:31.440 --> 15:36.800
And now what we have to do, we need to replace this player dot position with this target pose.

15:36.800 --> 15:37.960
And this target pose.

15:37.960 --> 15:41.720
Vector is just gonna follow our player in the x axis only.

15:41.720 --> 15:44.760
And it's not gonna do in the y axis.

15:44.760 --> 15:48.440
So we're gonna replace this player dot position basically the target pose.

15:48.560 --> 15:53.000
So this vector2 dot move towards function as a second argument.

15:53.000 --> 15:54.720
It's gonna take the target pose.

15:54.720 --> 15:58.190
So we will pass our this pose which is our target post.

15:58.190 --> 16:00.190
So we're going to pass our target post.

16:00.190 --> 16:05.750
And we do know that this target post parameter, this vector to target post parameter.

16:05.750 --> 16:07.550
Just going to follow our player.

16:07.590 --> 16:11.230
Basically this player GameObject in the x axis only.

16:11.230 --> 16:14.830
And now we're going to press down Ctrl S in order to save our script.

16:14.830 --> 16:16.710
And now let's move over to unity.

16:16.870 --> 16:22.510
And by now we're not gonna be able to see or not gonna able to notice that thing.

16:22.830 --> 16:29.750
Basically, our enemy two GameObject not gonna follow our player GameObject in the Y axis or vertically.

16:29.790 --> 16:33.390
It's gonna follow in the x axis or horizontally.

16:33.390 --> 16:39.710
So that's why we just created that factor two parameter to just follow our player, uh, GameObject

16:39.710 --> 16:41.470
in the x axis only.

16:41.630 --> 16:47.190
And now if we go ahead and then hit this play button in the top, you will see this play this enemy

16:47.190 --> 16:48.030
two game object.

16:48.030 --> 16:54.590
By now it's gonna follow our player GameObject in the horizontally or in the x axis only.

16:54.590 --> 16:58.430
So let's have a look on this Is and we're going to jump over it.

16:58.470 --> 17:04.070
And we can see by now this enemy two game object is not jumping in.

17:04.110 --> 17:08.470
Basically, it's not moving in the y axis or vertically.

17:08.670 --> 17:16.710
And when once we get in attack basically closer to this enemy to game object or this enemy to game object,

17:16.750 --> 17:18.870
get closer to this player game object.

17:18.870 --> 17:25.790
As you can see, we just printing out a message in our console tab says play a play enemy to attack

17:25.790 --> 17:26.830
animation clip.

17:26.830 --> 17:32.990
But once we get a little bit away from this enemy two game object, we can see it's now changing because

17:32.990 --> 17:35.990
the position is greater than retrieved distance.

17:35.990 --> 17:42.670
But the position between our player and this enemy to if it's lesser than retrieved distance than what

17:42.670 --> 17:43.310
we're doing.

17:43.310 --> 17:49.190
So in that moment we just printing out the message in the console says play enemy to attack animation.

17:49.230 --> 17:53.390
Basically, we can simply assume that the distance is too much close.

17:53.390 --> 17:59.550
So that's why we just printing out this message instead of chasing our player game object, but the

17:59.550 --> 18:05.190
distance between our this enemy to and this player, if it's greater than retrieve distance basically

18:05.190 --> 18:13.710
means sorry and basically means the basically means that our player and this enemy to basically little

18:13.750 --> 18:15.030
bit further away.

18:15.030 --> 18:21.510
So in that moment, if I just cut this and then move over to my enemy to script what we can, what we're

18:21.510 --> 18:27.470
doing in that moment, we're just chasing our player game object in the x axis only, as you can see,

18:27.510 --> 18:34.030
if the distance between our enemy and this player game object, if it's greater than retrieve distance,

18:34.030 --> 18:39.910
basically means the distance between our player and this enemy to is not too much close yet.

18:39.910 --> 18:43.270
So that's why we chasing our player game object.

18:43.270 --> 18:51.150
But if it's too much close, basically the the distance between is now lesser than this retrieve distance

18:51.150 --> 18:52.190
so else condition.

18:52.190 --> 18:59.300
If the if our enemy to game object and the player game object is still to, uh, basically, uh, too

18:59.300 --> 19:00.060
much close.

19:00.100 --> 19:06.460
Then we're just printing out this message in the console, says play Enemy to attack animation message

19:06.460 --> 19:08.060
in our console tab.

19:08.060 --> 19:14.940
So I hope you get the idea that what we are just doing and what we have to do, uh, we now need to

19:14.940 --> 19:18.820
play the attack animation clip of this enemy two game object.

19:18.820 --> 19:25.060
Once it's get in attack range, and if the distance between is now too much close, then this enemy

19:25.060 --> 19:31.780
two game object need to play the attack to animation clip instead of playing the walking animation clip

19:31.780 --> 19:33.460
over again and again.

19:33.460 --> 19:38.500
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

19:38.500 --> 19:44.180
you select your enemy two game object and this player then go over to Inspector Overrides and then apply

19:44.180 --> 19:44.620
all.

19:44.620 --> 19:51.740
So if you change something around inside this enemy two script or anything or with any component, so

19:51.740 --> 19:57.060
the changes can just gonna be saved into this enemy two GameObject prefab as well.
