WEBVTT

00:00.040 --> 00:06.240
Our player is not flipping to the left direction once it starts moving to the left direction, so we

00:06.240 --> 00:07.120
need to flip that.

00:07.120 --> 00:08.760
But before we we do.

00:08.800 --> 00:09.680
Let me show you.

00:09.840 --> 00:16.040
So I'm going to click, uh click on this play button in the top in order to start our game.

00:16.040 --> 00:17.880
And let's have a look on this.

00:19.080 --> 00:24.240
So we should able to see that we're not gonna flip to the left direction.

00:24.240 --> 00:25.760
So we need to flip that.

00:26.000 --> 00:27.480
So let me show you first.

00:27.680 --> 00:33.840
So as you can see we are moving to the left direction, but we're not flipping at all to the left direction.

00:34.040 --> 00:41.160
So if I select my player in the hierarchy then move over to Inspector Transform component and then rotation

00:41.160 --> 00:43.040
and go with y axis.

00:43.040 --> 00:46.400
And if I put in the y something like -180.

00:46.600 --> 00:52.080
As you can see, as soon as we put -180 it's flipped to the left direction.

00:52.080 --> 00:58.800
So basically what we have to do, we need to set the Euler angles in the y to -180 as you can see.

00:59.080 --> 01:00.720
So let's get out of play mode.

01:01.000 --> 01:03.380
And for that we will create a variable.

01:03.380 --> 01:06.460
We're just going to keep track of which direction we're looking.

01:06.580 --> 01:09.900
So let's open up our player script by double clicking on it.

01:10.260 --> 01:13.100
And let's go to the top of our class.

01:13.260 --> 01:14.700
Somewhere around here.

01:14.980 --> 01:20.220
And that parameter doesn't need to be public so we can make it private instead.

01:20.340 --> 01:23.620
So this will be private private.

01:23.620 --> 01:26.580
And this will be bool which can be true or false.

01:26.700 --> 01:29.260
And let's call this one facing right.

01:29.500 --> 01:30.940
Facing right.

01:30.940 --> 01:33.220
And let's close that up with semicolon.

01:33.220 --> 01:35.740
And we will move over to this start function.

01:35.740 --> 01:39.380
So we do know that start function get call in this start of the game.

01:39.380 --> 01:42.860
So in this start of the game I'm going to say facing right to true.

01:43.100 --> 01:45.140
And let's close that up with semicolon.

01:45.140 --> 01:50.180
So let me explain why we created this parameter and why we set it equal to true.

01:50.620 --> 01:55.260
So this parameter we just created to keep track of which direction we're looking.

01:55.260 --> 01:57.340
So we're looking to the right direction.

01:57.340 --> 02:01.420
So that's why I just named that parameter to something like facing right.

02:01.420 --> 02:04.340
And in the start of the game I just said facing right.

02:04.440 --> 02:05.440
equal to true.

02:05.440 --> 02:08.240
As you can see, we're looking to the right direction.

02:08.240 --> 02:13.920
So that's why uh, so that's why in the start of the game we just said facing right.

02:13.960 --> 02:14.480
True.

02:14.480 --> 02:19.160
But once we're gonna start moving to the left direction, we need to flip this player.

02:19.320 --> 02:22.760
Uh, so what we have to do, we're going to make a function for it.

02:22.760 --> 02:27.880
So let's go to our script and let's make the function somewhere around here.

02:28.240 --> 02:30.800
And for function we're going to type void.

02:31.000 --> 02:33.440
And let's call this function to something like flip.

02:33.440 --> 02:36.400
Let's make first bracket and some curly brackets.

02:36.400 --> 02:38.360
And what we want to check over here.

02:38.400 --> 02:42.640
We basically want to check if we're moving to the left direction or not.

02:42.640 --> 02:48.880
And we do know that that movement parameter right over here, as you can see, we created to just,

02:48.920 --> 02:51.280
uh, which direction we're going.

02:51.560 --> 02:54.600
Uh, this right over here is responsible.

02:54.920 --> 03:00.200
Uh, we do know that if the movement is something like minus one means we're holding down a key.

03:00.400 --> 03:03.520
So this player will start moving to the left direction.

03:03.520 --> 03:05.800
So that movement is just gonna be minus one.

03:05.800 --> 03:10.210
Basically means we're just gonna move or start moving to the left direction.

03:10.210 --> 03:13.290
So we need to flip our player to the left direction.

03:13.930 --> 03:17.450
So now what we're gonna do, we're gonna move over to this flip function.

03:17.450 --> 03:19.210
And over here we will check.

03:19.210 --> 03:23.770
So let's make if condition, let's make first bracket and some curly brackets.

03:23.770 --> 03:25.730
So what we want to check over here.

03:25.730 --> 03:30.490
We basically want to check if our movement is lesser than zero.

03:30.850 --> 03:37.050
And we do note that once we press down a key on our keyboard, that movement just gonna, just gonna

03:37.050 --> 03:37.970
be minus one.

03:37.970 --> 03:45.610
And we do know that if this if this movement is minus one and minus one is lesser than zero, it basically

03:45.610 --> 03:46.610
means true.

03:47.010 --> 03:50.290
Minus one means that we are moving to the left direction.

03:50.290 --> 03:52.610
So what we need to do, we need to flip.

03:52.770 --> 03:56.170
But we also want to check if we started moving to the left.

03:56.170 --> 03:59.410
Direction basically means that movement is minus one.

03:59.410 --> 04:02.450
And we do know that minus one is lesser than zero f.

04:02.490 --> 04:07.650
At the same time, if we are looking to the right direction, we started moving to the left direction,

04:07.650 --> 04:11.070
but we are looking to the right direction, then what we have to do?

04:11.070 --> 04:13.670
We need to flip this player to the left direction.

04:13.670 --> 04:15.390
So also we want to check.

04:15.390 --> 04:17.710
So we're going to use this an end condition.

04:17.710 --> 04:18.910
And we will check.

04:19.310 --> 04:25.670
We will check that if facing right is equals equals to true basically means we're looking to the right

04:25.710 --> 04:26.550
direction.

04:26.550 --> 04:27.990
So now what we have to do.

04:27.990 --> 04:29.510
We need to flip this player.

04:30.150 --> 04:34.750
So to in order to flip that we're going to move over to this rotation.

04:34.750 --> 04:37.590
Or you can simply say Euler angles in the y.

04:37.670 --> 04:40.630
We're going to set to something like -180.

04:40.670 --> 04:46.150
And we do know that once we set -180 this player flips to the left direction.

04:46.150 --> 04:48.310
So what we will do in this situation.

04:48.750 --> 04:50.790
So we will go to transform.

04:50.790 --> 04:57.390
Then we're going to access the Euler angles which going to have x x y and z axis.

04:57.510 --> 05:00.990
And we're going to set equal to a new Vector3 y.

05:00.990 --> 05:02.830
We set it equal to new Vector3.

05:02.870 --> 05:09.110
Since this transform dot Euler angles just going to have x axis, y axis, and the z axis.

05:09.270 --> 05:15.610
And this new Vector3 is also going to have the x axis, y axis, and the z axis.

05:15.610 --> 05:17.850
And let's close that off with semicolon.

05:18.130 --> 05:20.450
So we want to flip to the left direction.

05:20.450 --> 05:24.690
So in the y part we need to do we basically need to put minus one ATF.

05:24.730 --> 05:27.330
So we're going to put minus one ATF in the Y.

05:27.690 --> 05:31.210
And we're not going to do at all anything in the x axis.

05:31.210 --> 05:32.770
So we're going to put zero f.

05:32.770 --> 05:35.730
And we will do the same thing in the z axis.

05:35.970 --> 05:39.290
So once we flip our player to the left direction.

05:39.490 --> 05:45.770
Uh so if we flip to the left direction, as you can see it will no longer able to look to the right

05:45.770 --> 05:46.450
direction.

05:46.450 --> 05:50.770
So that's why after flipping we will set that facing right to false.

05:50.770 --> 05:55.290
Because in that moment we're not going to look towards to the right direction.

05:55.290 --> 05:56.130
So that's why.

05:56.410 --> 06:04.410
So after flipping so after flipping we're going to set facing right equal to false because we no longer

06:04.410 --> 06:06.890
able to look to the right direction.

06:07.050 --> 06:10.130
And now we're going to press down Ctrl S to save our script.

06:10.130 --> 06:12.010
And now let's move over to unity.

06:12.110 --> 06:15.950
Firstly, we're checking if movement is lesser than zero f or not.

06:15.990 --> 06:21.710
Means that if we started moving to the left direction or not at the same time we checking to the right.

06:22.110 --> 06:27.910
If we're looking to the right direction, then firstly moving over to this player transform, then Euler

06:27.910 --> 06:30.630
angles and we're setting y to -180.

06:30.670 --> 06:33.590
So it's just going to flip to the left direction.

06:33.590 --> 06:35.390
So let's have a look on this.

06:37.230 --> 06:39.510
But we're gonna get nothing.

06:39.550 --> 06:40.750
Let me show you why.

06:41.310 --> 06:44.030
So just wait a moment to compile our script.

06:44.030 --> 06:47.630
And once it's done, we can just check it right away.

06:47.870 --> 06:52.110
Once our compilation is done for our script.

06:53.110 --> 06:54.990
So just wait a little moment.

06:54.990 --> 07:02.510
And now if we go ahead and then click on this play button in the top, you will see nothing gonna happen.

07:02.510 --> 07:07.510
Even if we press down a key Y, because we also need to call that function.

07:07.510 --> 07:10.950
We basically made that flip function, but we never calling it.

07:10.950 --> 07:17.120
So if I press down a key, as you can see, it's not looking to the left direction because we also need

07:17.120 --> 07:18.560
to call the flip function.

07:18.560 --> 07:24.200
So let's get out of play mode and where we need to call it, we need to call it this update function.

07:24.200 --> 07:29.680
Because this update function runs every single frame and every single frame, we need to check which

07:29.680 --> 07:35.480
direction we going or, uh, which direction we simply moving on.

07:35.920 --> 07:37.920
So we're going to call it something over here.

07:37.920 --> 07:40.080
So let's call the flip function here.

07:40.080 --> 07:46.600
And then close that off with semicolon after setting up the movement right over here, as you can see.

07:46.640 --> 07:54.200
And if I save my script and then move over to unity, you will see that as soon as we gonna start moving

07:54.240 --> 07:59.400
to the left direction, we will see our player to flip to the left direction.

07:59.440 --> 08:05.760
Since we setting the Euler angles in the y to minus one 8180, let me show you.

08:05.760 --> 08:09.840
So let's click on this play button in the top in order to start our game.

08:09.880 --> 08:12.040
And let's, uh, check it.

08:15.520 --> 08:17.340
So just wait a moment.

08:17.740 --> 08:24.420
And now, if I hold down a key, as you can see, as soon as we press down a key, uh, this player

08:24.420 --> 08:26.420
just flips to the left direction.

08:26.580 --> 08:34.100
But if I, uh, right now start moving to the right direction, it's not, uh, flipping to the right

08:34.140 --> 08:36.900
direction at all after flipping to the left.

08:36.940 --> 08:43.300
So what we basically have to do if we select our player in the hierarchy, move over to Inspector transform

08:43.300 --> 08:46.820
and rotation in the Y or Euler angles in the y.

08:46.860 --> 08:51.860
If we set back to zero, as you can see, it's flipped to the right direction.

08:51.860 --> 08:56.300
So basically we need to check if we just moving to the right direction.

08:56.300 --> 08:59.820
Then we basically have to set back this y to zero.

08:59.820 --> 09:02.580
So it's going to flip to the right direction.

09:02.580 --> 09:07.020
And after flipping we also need to set that facing right parameter to true.

09:07.060 --> 09:07.460
Why?

09:07.580 --> 09:12.220
Because as you can see in this situation we're gonna look to the right direction.

09:12.220 --> 09:15.020
So that's why we need to set facing right to true.

09:15.020 --> 09:21.360
So let's get out of play mode and let's move over to script and let's move over to our flip function.

09:21.480 --> 09:29.240
So over here we will check else if else if our movement movement is greater than zero f.

09:29.360 --> 09:35.560
And we do know that if movement is greater than zero f basically means we holding down D means we started

09:35.560 --> 09:37.160
moving to the right direction.

09:37.400 --> 09:41.560
And also we will check if we're not looking to the right direction.

09:41.560 --> 09:45.360
Basically this player have just looking to the left direction.

09:45.360 --> 09:52.720
So also we will check if we started moving right direction and we not uh, looking to the right direction

09:52.720 --> 09:55.880
basically means facing right equals equals to false.

09:56.120 --> 10:02.360
If that is the case then what we're going to do, we're going to set back our Euler angles to Euler

10:02.360 --> 10:04.080
angles in y to zero.

10:04.080 --> 10:06.760
So I'm going to copy this line of code by Ctrl C.

10:06.960 --> 10:09.840
And let's paste it over here by Ctrl V.

10:10.320 --> 10:14.760
And so what we will do firstly we will go to transform then Euler angles.

10:14.920 --> 10:17.800
And in the y we're going to set back to zero f.

10:18.000 --> 10:20.900
And after flipping to the right direction.

10:20.940 --> 10:27.020
We need to set the spacing right parameter to true instead of false, because in that situation, we're

10:27.020 --> 10:28.620
going to look to the right direction.

10:28.620 --> 10:30.860
As you can notice right over here.

10:31.220 --> 10:32.580
So firstly what we're doing.

10:32.620 --> 10:36.100
Firstly we're checking if our movement is greater than zero.

10:36.380 --> 10:38.860
Basically means we're moving to the right direction.

10:39.100 --> 10:44.100
And we do know that if we hold down D key that movement just gonna be one.

10:44.100 --> 10:46.700
So one basically is greater than zero.

10:47.060 --> 10:48.460
And we don't know that.

10:48.460 --> 10:51.340
And also we're checking if facing right is false.

10:51.340 --> 10:54.540
Basically means this player is looking to the left direction.

10:54.540 --> 10:58.460
Then what we're doing we're just setting the Y to zero F.

10:58.460 --> 11:01.860
And after that we're just setting facing right parameter to true.

11:01.900 --> 11:06.660
Because in that in this situation, this player just gonna look to the right direction.

11:06.660 --> 11:09.740
So that's why we uh, we just set facing.

11:09.740 --> 11:10.180
Right.

11:10.180 --> 11:10.540
True.

11:10.780 --> 11:14.100
And if you just want to don't do it like this.

11:14.140 --> 11:20.420
If facing right and if, if you just put exclamation mark, it basically means the same thing.

11:20.500 --> 11:23.920
Facing right basically means facing right is false.

11:24.000 --> 11:25.600
It's basically the same thing.

11:25.640 --> 11:27.520
Or you can just simply do it.

11:27.560 --> 11:29.720
Equals equals to false.

11:29.920 --> 11:34.680
And if you just want to check with true you can just leave it like this.

11:34.720 --> 11:37.480
It's also basically means true true.

11:37.480 --> 11:43.400
And if you just put exclamation mark right in front of this, facing right basically means it false.

11:43.560 --> 11:50.000
So firstly we're checking if movement is greater than zero means we started moving to the right direction.

11:50.000 --> 11:53.880
And if we're looking to the left direction then firstly we flipping.

11:53.920 --> 11:57.800
And after flipping we just setting back facing right parameter to true.

11:57.840 --> 11:58.920
As you can see.

11:59.120 --> 12:05.880
So now if I press down Ctrl s to set my script and then move over to unity, you will able to notice

12:05.880 --> 12:11.800
that as soon as we're gonna start moving to the right direction, our player just gonna flip to the

12:11.800 --> 12:12.680
right direction.

12:12.720 --> 12:17.960
Since we setting Euler angles in the y to zero or back to zero.

12:18.120 --> 12:23.530
So now if we go ahead and then click on this play button in the top to play our game.

12:23.530 --> 12:25.090
We can check that out.

12:25.570 --> 12:27.930
Uh, firstly, let's start moving.

12:28.130 --> 12:28.770
Moving.

12:28.770 --> 12:33.010
Or let's begin with a key basically to the left direction.

12:33.010 --> 12:33.850
So if I move.

12:33.890 --> 12:36.890
As you can see, it's moving to the left direction.

12:36.890 --> 12:39.290
And player just flipped to the left direction.

12:39.410 --> 12:45.610
And now if I start moving to the right direction we can see player flips to the right direction.

12:45.850 --> 12:51.650
And if we start moving one more time again to the left direction, we can see it's flipping to the left

12:51.650 --> 12:55.970
direction and it's flipping to the right direction as well.

12:56.010 --> 12:59.970
Basically, this player is flipping in the direction it's going.

13:00.010 --> 13:02.610
So basically that is a pretty great thing.

13:02.770 --> 13:09.730
And let's suppose you still want to see this, uh, private, uh, this right over here, this private

13:09.730 --> 13:13.970
facing right parameter, uh, underneath your player script.

13:13.970 --> 13:19.650
Then how you can do it for, uh, to do it, just go over to Inspector in the top right you can see

13:19.690 --> 13:20.210
three dots.

13:20.210 --> 13:23.290
So just click there and then click on this debug mode.

13:23.290 --> 13:28.030
And as soon as we have chosen we can see our facing right parameter.

13:28.030 --> 13:30.670
But I'm gonna go back to my normal mode.

13:30.670 --> 13:36.190
So you can just click on that three dot once again in order to go back in normal mode.

13:36.190 --> 13:40.950
And I'm gonna get out of play mode by just clicking on that play button.

13:40.950 --> 13:46.910
So I hope you get the idea that how you can flip your player in the direction player is going.

13:46.910 --> 13:49.310
So I hope you get you get the idea.

13:49.310 --> 13:54.030
And also we need to make the idle animation clip and all of those things.

13:54.430 --> 13:59.550
And now I'm gonna press down Ctrl s on my keyboard in order to save my project.

13:59.710 --> 14:06.270
Or we can simply move over to top left file in unity editor, then click on the save button in order

14:06.270 --> 14:07.950
to save our project.

14:08.190 --> 14:14.390
And make sure if you've done any changes in in this player GameObject in the hierarchy, then make sure

14:14.390 --> 14:20.230
you go over to Inspector Overrides and then apply all the changes you made over here.

14:20.270 --> 14:23.950
It will be save this player GameObject prefab as well.

14:24.590 --> 14:27.910
So now I'm going to press Ctrl S to save my project.
