WEBVTT

00:00.280 --> 00:01.600
We have a little bug.

00:01.640 --> 00:03.880
Let me show you that to you.

00:03.880 --> 00:07.200
So let's click on this play button in the top to play our game.

00:07.520 --> 00:10.480
Let's jump over top of this anime game.

00:10.520 --> 00:12.080
Object anime.

00:12.080 --> 00:15.880
Just not gonna look towards in the direction player is going.

00:15.880 --> 00:19.840
So we need to flip our enemy in the direction player is going.

00:19.880 --> 00:24.000
Once our player gets in attack range of this enemy one.

00:24.000 --> 00:24.800
Let me show you.

00:24.800 --> 00:27.720
As you can see, it's playing the arrow animation clip.

00:27.720 --> 00:34.480
So if I jump over on it, uh, as you can see, enemy is not looking to the right direction.

00:34.800 --> 00:36.840
The player is just going.

00:36.840 --> 00:40.480
So we need to flip this enemy in the right direction as well.

00:40.520 --> 00:46.640
If the, uh, if the player is, uh, player is going to the right direction.

00:46.640 --> 00:48.360
So let's get out of play mode.

00:48.360 --> 00:53.800
So what we will do, we're just going to create a parameter to just keep track of which direction this

00:53.840 --> 00:55.520
enemy is facing.

00:55.520 --> 01:00.840
And then we're going to check we also need our player transform or basically player position.

01:01.170 --> 01:09.330
And we also are just going to create our parameter in order to check which direction this enemy is facing.

01:09.410 --> 01:13.130
So let's open up our enemy script by double clicking on it.

01:13.130 --> 01:17.290
And let's go to top of our class and let's create those two variables.

01:17.370 --> 01:20.210
So the first thing we need our player position.

01:20.210 --> 01:22.730
So let's create another serialized field.

01:23.930 --> 01:28.650
For position we need to type transform serialized field private.

01:29.010 --> 01:32.610
And this will be transform simply position.

01:32.610 --> 01:35.770
And we can call this one something like player position.

01:35.770 --> 01:37.610
Or let's simply call it player.

01:37.850 --> 01:41.290
And the second thing we need are our parameter.

01:41.290 --> 01:45.890
In order to keep track of which direction our enemy is facing.

01:45.890 --> 01:48.410
So let's create another private parameter.

01:48.410 --> 01:53.090
It doesn't need to be public and that will be bool, which can be true or false.

01:53.090 --> 01:57.570
And we can call this one something like facing left facing left.

01:57.570 --> 01:59.770
And we're going to close that up with semicolon.

01:59.970 --> 02:02.460
And we will move over to this stat function.

02:02.460 --> 02:06.260
So we do know that stat function get called in the start of the game.

02:06.260 --> 02:10.500
So in this start of the game we're gonna set facing left equal to true.

02:10.780 --> 02:12.900
And let's close that up with semicolon.

02:12.900 --> 02:14.180
Why we have done this.

02:14.180 --> 02:16.060
If we move over to unity.

02:16.100 --> 02:19.140
As you can see we're looking to the left direction.

02:19.140 --> 02:23.740
So that's why facing left in the start of the game I just set equal to true.

02:23.780 --> 02:24.820
As you can see.

02:24.980 --> 02:27.740
Now we're going to save our script by Ctrl s.

02:27.780 --> 02:31.140
Now let's move over to unity and let's have a look.

02:31.500 --> 02:35.740
So we will be able to notice that uh notice that.

02:35.740 --> 02:42.940
So underneath our enemy one script a another filter is gonna pop up and it will, it will say something

02:42.940 --> 02:44.340
like player transform.

02:44.340 --> 02:48.980
So we need to drag our player transform or player position into that field.

02:48.980 --> 02:53.100
So we will have the player reference, uh player position reference.

02:53.100 --> 02:58.540
And after that we can just flip our enemy one in the direction player is going.

02:58.740 --> 03:05.300
So just wait a moment and once it's done, we can just drag the reference into that slot underneath

03:05.300 --> 03:06.980
our enemy one script.

03:07.140 --> 03:13.500
So now our script just gets compiled, as you can see, and we can see underneath our enemy.

03:13.540 --> 03:19.180
One script underneath our attack title player and it says None Transform.

03:19.180 --> 03:23.660
So let's select the player GameObject in the hierarchy and let's drag it over to that field.

03:23.700 --> 03:29.340
Now we can see we have the player transform, or you can simply say we have player position and now

03:29.340 --> 03:30.660
we can just flip that.

03:30.660 --> 03:37.020
So let's open up our enemy one script one more time, and let's make a function in order to basically

03:37.020 --> 03:38.340
flip our enemy.

03:38.340 --> 03:40.660
So let's make it somewhere around here.

03:40.940 --> 03:43.220
So for function we're going to type y.

03:43.260 --> 03:45.940
And we can call this function to something like flip.

03:46.140 --> 03:48.940
And let's make first bracket and some curly brackets.

03:49.140 --> 03:51.380
And what we want to check over here.

03:51.380 --> 03:57.820
We basically want to check that this uh let's suppose if I select my player and then select this move

03:57.860 --> 03:58.860
tool in the scene view.

03:58.860 --> 04:05.990
And let's move it somewhere around here, as you can see in this In this time, the player position

04:05.990 --> 04:10.910
in the x axis will be greater than position in the x axis, as you can see.

04:11.110 --> 04:18.390
So enemy x position will be lesser than enemy player X position less than our player x position.

04:18.670 --> 04:22.510
So what we can simply do right over here we we will check.

04:22.510 --> 04:23.750
So first thing we will check.

04:23.750 --> 04:27.630
So let's make if condition then first bracket and some curly brackets.

04:27.710 --> 04:29.710
So what we want to check over here.

04:29.710 --> 04:38.390
We basically want to check if our transform dot position dot x if it's lesser than player.

04:38.390 --> 04:40.990
So we will going to player than position.

04:40.990 --> 04:43.190
And we're going to access the x axis.

04:43.270 --> 04:49.270
So what we're doing over here we basically checking if a transform dot position dot x basically means

04:49.270 --> 04:51.470
enemy position in the x axis.

04:51.510 --> 04:54.670
If it's lesser than player position in the x axis.

04:54.670 --> 05:00.390
As you can see, we are going over to player and then accessing the position and then accessing the

05:00.390 --> 05:07.080
x position, and we checking if our enemy position in the x axis is lesser than player position in the

05:07.080 --> 05:08.000
x axis.

05:08.000 --> 05:13.720
So if the player is somewhere around here, as you can see, it's going to be play enemy position in

05:13.760 --> 05:19.040
the x axis will be lesser than player position in the x axis, as you can see.

05:19.440 --> 05:24.360
At the same time, we will check if our enemy is facing to the left direction.

05:24.360 --> 05:28.920
If that is the case, then we need to flip our enemy to the right direction.

05:28.920 --> 05:30.600
So we also want to check.

05:30.960 --> 05:31.960
So what we will do.

05:31.960 --> 05:34.160
So we're going to put this an end condition.

05:34.160 --> 05:37.880
And we will check if our facing left is true.

05:37.880 --> 05:44.120
So you can if you just leave it like this it basically means true basically means that this enemy is

05:44.120 --> 05:45.720
looking to the left direction.

05:45.720 --> 05:51.520
And we do know that in the start of the game, we just said that facing left to true means we are looking

05:51.520 --> 05:52.680
to the left direction.

05:52.880 --> 05:58.280
So right now what we have to do, we need to flip this enemy to the right direction, and we need to

05:58.320 --> 06:01.240
set that facing left parameter to true false.

06:01.440 --> 06:05.850
Uh, because once we gonna flip this admin to the right direction?

06:05.850 --> 06:08.650
Facing left parameter will be false base.

06:08.770 --> 06:15.410
Or you can say this enemy one will be no longer able to look to look towards to the left direction.

06:15.410 --> 06:16.610
So that is the thing.

06:16.810 --> 06:18.290
And to flip this enemy.

06:18.290 --> 06:24.250
So if I select my enemy one inch the hierarchy, then go over to Inspector Transform and then rotation

06:24.250 --> 06:25.130
and in the y.

06:25.130 --> 06:31.970
If we set it to something like -180, as you can see, as soon as we put this enemy just gets flipped

06:31.970 --> 06:33.330
to the right direction.

06:33.370 --> 06:37.930
Basically, we're doing the same thing as we have done inside this player script.

06:37.930 --> 06:39.650
So we're gonna do the same thing.

06:39.650 --> 06:41.010
So I'm going to set back to zero.

06:41.010 --> 06:44.250
So we basically have to access the Euler angles.

06:44.250 --> 06:47.690
So I'm going to select my player and let's move it over here.

06:48.050 --> 06:54.930
So if our enemy position in the x axis is lesser than player position in the x axis, at the same time

06:54.970 --> 06:57.570
our enemy is looking to the left direction.

06:57.570 --> 06:59.050
So if that is the case.

06:59.050 --> 07:01.890
So what we need to do, we need to flip our enemy.

07:02.090 --> 07:05.740
And in order to flip our enemy, we need to use this transformer.

07:05.780 --> 07:11.100
Euler angles which gonna have x x axis, y axis and the z axis.

07:11.100 --> 07:20.100
So we will set equal to a new vector three which gonna have x axis y axis, sorry y axis and z axis.

07:20.100 --> 07:22.180
And let's close that up with semicolon.

07:22.180 --> 07:28.340
So if we put in the y -180 as you seen it's just gonna flips to the right direction.

07:28.340 --> 07:30.700
So in the Y we're going to put -188.

07:30.860 --> 07:33.060
And in the zero we don't sorry.

07:33.100 --> 07:36.660
In the z axis we don't want to do anything at all.

07:36.660 --> 07:39.580
So that's why we're gonna put zero f in the z axis.

07:39.580 --> 07:42.620
And we will do the same thing in the x axis.

07:42.620 --> 07:49.500
And after flipping this enemy to the right direction, we need to set that facing left parameter to

07:49.540 --> 07:49.980
false.

07:49.980 --> 07:50.420
Why?

07:50.460 --> 07:54.820
Because this enemy will no longer able to look towards to the left direction.

07:54.820 --> 08:00.860
So that's why after flipping, we're gonna, uh, we're gonna set that facing left parameter to false.

08:00.860 --> 08:05.500
So after flipping we will set our facing left lab equal to false.

08:05.940 --> 08:07.980
Let's set equal to false.

08:08.140 --> 08:10.220
And now we need to call this flip function.

08:10.220 --> 08:12.660
But when we should call this flip function.

08:12.660 --> 08:17.940
Once this player game object gets in attack range of this enemy game object.

08:17.980 --> 08:19.980
Basically we need to call it over here.

08:19.980 --> 08:25.660
As you can see, if call info true basically means our player gets in attack range.

08:25.700 --> 08:28.540
Now what we will do we will call the flip function.

08:28.540 --> 08:29.700
Let's make first bracket.

08:29.700 --> 08:32.540
Then close that up with semicolon and flip function.

08:32.540 --> 08:34.540
Just gonna do this do these things.

08:34.540 --> 08:39.660
Firstly it's going to check the distance uh distance in the x axis.

08:39.700 --> 08:42.940
If it's lesser than player position in the x axis.

08:42.980 --> 08:46.060
At the same time enemy is looking to the left direction.

08:46.060 --> 08:52.660
Then we flipping our enemy to the right direction and after flipping we just setting setting this facing

08:52.660 --> 08:53.380
left to false.

08:53.380 --> 08:57.260
Because in that moment we're going to look towards to the right direction.

08:57.260 --> 09:00.460
So that's why we set facing left parameter to false.

09:00.460 --> 09:03.620
Once we flip our enemy to the right direction.

09:03.620 --> 09:06.590
And now we're going to press down Ctrl S to save our script.

09:06.590 --> 09:08.470
And now let's move over to unity.

09:08.870 --> 09:15.630
So as soon as our player just gonna surpass our enemy one GameObject in the x axis, you will see this

09:15.670 --> 09:20.670
enemy just flips to the right direction or in the direction the player is going.

09:20.710 --> 09:23.350
Basically, it's gonna flip to the right direction.

09:24.030 --> 09:29.630
And so just wait a little moment and once it's done, we can just go ahead and check.

09:29.910 --> 09:30.990
Check that out.

09:31.310 --> 09:38.430
And now I'm going to take my player and I'm gonna move it a little bit further away from this enemy

09:38.470 --> 09:39.510
one GameObject.

09:39.510 --> 09:43.630
So let's click on this play button in the top in order to play our game.

09:43.630 --> 09:45.590
And let's have a look on this.

09:48.990 --> 09:56.350
So just wait a moment and let's get in attack range and let's surpass this enemy.

09:56.350 --> 10:02.910
So if we surpass this enemy, as you can see, as soon as we have done it, enemy is now looking to

10:02.950 --> 10:04.150
the right direction.

10:04.350 --> 10:10.440
But if we move to the move over to the left direction, this enemy is not flipping at all.

10:10.480 --> 10:12.720
Back to our left direction.

10:12.720 --> 10:15.280
So we also need to set this y to zero.

10:15.400 --> 10:21.080
So if I select my enemy one inch the hierarchy, then go over to Inspector and set this y rotation to

10:21.120 --> 10:21.560
zero.

10:21.680 --> 10:26.440
As you can see, as soon as we're going to do it, it's going to flip to the left direction one more

10:26.440 --> 10:26.840
time.

10:26.880 --> 10:28.280
Basically this enemy.

10:28.280 --> 10:30.760
So we also need to set back this y to zero.

10:30.920 --> 10:35.080
And also we need to set back our facing lab parameter to true.

10:35.120 --> 10:35.520
Why?

10:35.720 --> 10:40.520
Because as you can see in this moment we will look towards to the left direction.

10:40.520 --> 10:43.240
So that's why we need to flip our sorry.

10:43.360 --> 10:46.800
So that's why we need to set facing lab parameter to true.

10:46.840 --> 10:47.720
One more time.

10:47.720 --> 10:51.840
Because in this condition we just want to look towards to the left direction.

10:51.840 --> 10:57.600
So firstly we need to check the distance distance between them in the x axis as.

10:57.640 --> 10:59.120
Then we can just do that.

10:59.120 --> 11:02.720
So let's get out of play mode and let's move over to our script.

11:02.760 --> 11:04.520
And over here we will check.

11:04.520 --> 11:07.130
So we're going to use this else if condition.

11:07.130 --> 11:08.330
So else if.

11:08.370 --> 11:11.010
Let's make first bracket and some curly brackets.

11:11.010 --> 11:11.450
So what.

11:11.450 --> 11:12.930
We want to check over here.

11:12.930 --> 11:20.050
We basically want to check if our current position of this enemy is greater than x in the x axis, is

11:20.050 --> 11:27.370
greater than player position in the x axis, and basically means player is over here, then what?

11:27.370 --> 11:29.130
We need to do this, we need to.

11:29.170 --> 11:34.930
If the enemy is looking to the right direction, then firstly we need to set back this y to zero.

11:34.930 --> 11:39.010
And if we set it as you can see it's going to look towards to the left direction.

11:39.130 --> 11:42.850
And also we need to set back that facing left parameter to true.

11:42.890 --> 11:48.210
Because in this situation, as you can see we're gonna look towards to the left direction.

11:48.210 --> 11:50.210
So now we're going to do that thing.

11:50.210 --> 11:57.130
So else if we will check if our transform dot position dot x basically means current position of that

11:57.130 --> 12:03.570
enemy in the x axis a if it's a if it's greater than player position in the x axis.

12:03.570 --> 12:05.010
So we will go to player.

12:05.010 --> 12:09.380
Then we're going to access the position, then we're gonna access the X position.

12:09.380 --> 12:12.820
If it's the case, means player is somewhere over here.

12:12.820 --> 12:15.940
And sorry, enemy is somewhere over here.

12:15.940 --> 12:17.460
And player is here.

12:17.460 --> 12:24.260
So as you can see, player, an enemy in the x axis is going to be greater than player in the x axis.

12:24.260 --> 12:26.740
And we're checking over here as you can see.

12:26.900 --> 12:29.940
So current position of that enemy in the x axis.

12:29.980 --> 12:34.940
If it's greater than player position in the x axis at the same time we will check.

12:34.940 --> 12:42.300
So we're going to use this in condition and we will check if we just look if this enemy is looking to

12:42.300 --> 12:46.420
the right direction basically means facing left parameter is false.

12:46.420 --> 12:48.220
So what we will do we will check.

12:48.260 --> 12:52.460
If so you need to put this exclamation mark basically means a.

12:52.460 --> 12:56.460
And then you have to type the parameter name which is facing left.

12:56.500 --> 13:01.820
It basically means that it's simply means that facing left parameter is false.

13:01.820 --> 13:04.060
Or you can simply do these things as well.

13:04.060 --> 13:10.590
Which is facing left equals equals to false basically means that our enemy one is looking to the right

13:10.590 --> 13:13.710
direction, or we can simply do these things.

13:13.710 --> 13:15.950
It also means the same thing.

13:16.270 --> 13:20.270
We can just put exclamation mark right in front of this facing left.

13:20.430 --> 13:26.870
It basically means that facing left parameter is false, means our enemy is looking to the right direction.

13:26.870 --> 13:28.710
So right now what we have to do.

13:28.750 --> 13:31.070
Firstly, we need to set back y to zero.

13:31.310 --> 13:36.190
So this enemy one just going to flip towards to the left direction one more time.

13:36.190 --> 13:39.630
And after it we need to set that facing left to true.

13:39.630 --> 13:44.070
Because uh, if we flip our enemy to the left direction.

13:44.070 --> 13:48.310
So this enemy is just going to look towards to the left direction as you can see.

13:48.310 --> 13:49.150
So that's why.

13:49.190 --> 13:55.110
So we will simply copy this line of code by Ctrl C and let's paste it over here by Ctrl V.

13:55.310 --> 13:57.030
So we will set in the Y.

13:57.310 --> 13:59.510
So let's set back to zero F.

13:59.710 --> 14:05.150
And after flipping this enemy to the left direction we're going to set this facing left parameter to

14:05.190 --> 14:05.590
true.

14:05.590 --> 14:09.830
Because in this situation we're just going to look towards to the left direction.

14:09.990 --> 14:13.630
And now let's press down Ctrl S in order to save our script.

14:13.670 --> 14:15.750
And now let's move over to unity.

14:16.030 --> 14:23.950
So as soon as we're going to surpass our enemy one inch the x axis, then enemy one just looks to the

14:23.950 --> 14:24.710
right direction.

14:24.710 --> 14:30.590
But once our player is left direction from this enemy GameObject, then this enemy is just going to

14:30.590 --> 14:32.470
look towards to the left direction.

14:32.470 --> 14:38.030
Or you can simply say in the direction player is going enemy just gonna follow that.

14:38.070 --> 14:40.430
But or enemy just gonna flip that.

14:41.390 --> 14:45.310
So let's click on this play button in the top in order to play our game.

14:45.510 --> 14:49.510
And let's get inside attack range of this enemy one game object.

14:49.630 --> 14:55.110
So once we're gonna over here somewhere around here, then enemy is just going to look to the right

14:55.110 --> 14:55.870
direction.

14:55.870 --> 14:57.590
So let's take a look on this.

14:57.670 --> 15:04.150
So now if I surpass my player game object, as you can see, as soon as we doing this, enemy just looks

15:04.150 --> 15:05.510
to the right direction.

15:05.510 --> 15:08.400
But once we move to the left direction.

15:08.920 --> 15:10.640
We can see this enemy.

15:10.680 --> 15:14.520
It's now just a flip to the left direction as well.

15:14.520 --> 15:22.280
And if I just do it one more time, we can see now the the enemy just facing the direction in the direction

15:22.320 --> 15:23.520
player is going.

15:23.560 --> 15:30.680
Once our a our player in attack range, and if we just get outside of the attack range then this enemy

15:30.680 --> 15:31.840
just doing nothing.

15:31.880 --> 15:37.840
It just standing and then playing the idle animation clip over again and again.

15:37.840 --> 15:44.720
But once we get in attack range, it's just playing the attack arrow fire animation clip, and we can

15:44.720 --> 15:51.600
see that enemy one is now just looking towards or just flipping in the direction player is going, because

15:51.600 --> 15:58.120
we're doing inside this flip function, as you can see, and we only calling this flip function once

15:58.120 --> 16:02.200
our player get in attack range, as you can notice right over here.

16:02.720 --> 16:07.560
So I hope you get the idea that how you can do it and I'm gonna get out of play mode.
