WEBVTT

00:00.050 --> 00:00.560
Hello guys.

00:00.560 --> 00:01.580
Welcome to the video.

00:01.580 --> 00:07.310
In this one I want to show you how to flip the character because because right now if we try to run

00:07.310 --> 00:09.200
to the left he does not flip.

00:09.200 --> 00:10.760
And it feels weird, right?

00:10.760 --> 00:12.740
Let me explain what we can do here.

00:12.740 --> 00:17.030
So one of the ways is to flip the sprite over here.

00:17.030 --> 00:20.930
But actually I do not prefer this way for one simple reason.

00:20.930 --> 00:25.430
When we flip the sprite, we do not flip anything else except the sprite itself.

00:25.430 --> 00:32.090
So now my character is looking to the left, but his vector right, he's facing direction, goes to

00:32.090 --> 00:38.000
the right, and we cannot work with it because later on we're going to have a wall check for wall jump,

00:38.000 --> 00:44.330
which going to be casted to the right, and I want this check to be flipped as well when we flip in

00:44.330 --> 00:51.200
the character, because if you do it not like that, but if you rotate the character on the Y by 180.

00:52.810 --> 00:57.250
He also going to be facing left, but at the same time we're going to have his vector.

00:57.700 --> 01:00.550
Uh, to dot right, facing left.

01:00.550 --> 01:02.890
So we sort of know facing direction.

01:02.890 --> 01:06.940
We flip the facing direction and we flip sprite as well at the same time.

01:06.940 --> 01:08.470
That's why it is important.

01:08.470 --> 01:12.190
Now, how do we flip the character together with the facing direction.

01:12.190 --> 01:16.510
Actually I just showed you we're going to change rotation on the Y over here.

01:16.510 --> 01:21.070
And we're going to rotate character by 180 on the zero every time we want to flip him.

01:21.070 --> 01:26.650
And this is how it's going to look boom and then boom just like that.

01:26.650 --> 01:28.720
So let's go and do that in the script.

01:28.720 --> 01:32.320
Let's go and apply this rotation to the transform in the script.

01:36.030 --> 01:40.080
Um, somewhere all the way below but above on drawing gizmos.

01:40.260 --> 01:43.830
I'm going to make a method private void flip.

01:45.410 --> 01:47.090
Inside of this method I want to do.

01:47.120 --> 01:47.960
Transform.

01:47.960 --> 01:48.890
Rotate.

01:50.190 --> 01:57.840
Zero and x 180 on the Y and zero on the Z, so we only rotate him on the y axis.

01:58.170 --> 02:05.220
Now let's save this and let's just call this function to be sure it is working on the update I'm going

02:05.220 --> 02:10.560
to do if input get key down key code C clip.

02:10.560 --> 02:12.420
Just want to show you how it's going to work.

02:12.420 --> 02:13.440
Let's save this.

02:13.440 --> 02:14.550
Let's go back.

02:21.650 --> 02:27.170
And I'm standing here, I'm going to click the C button and I'm flipping.

02:27.590 --> 02:28.250
Okay.

02:28.250 --> 02:34.670
Now of course I cannot click this key on the keyboard every time I need to flip the character, I need

02:34.670 --> 02:37.400
to do it somehow, automatically.

02:37.580 --> 02:39.380
So this is how we're going to do it.

02:39.860 --> 02:47.360
We're going to make a method that will check if player is facing right and if he is facing right, but

02:47.360 --> 02:52.070
his velocity is negative and he running to the left means we need to flip him.

02:52.070 --> 02:57.590
Then if he's facing left, but he's supposed to be facing right because he's moving to the right, we're

02:57.590 --> 02:58.970
going to flip him again.

03:00.210 --> 03:01.740
For that over here.

03:01.740 --> 03:05.190
We need to make private boolean facing right.

03:05.190 --> 03:08.940
And we can give it a default value by just typing equals to true.

03:12.970 --> 03:17.260
So from the start this boolean will be true and we know that we are facing right.

03:17.590 --> 03:19.480
Now let's go.

03:21.360 --> 03:23.160
Somewhere here.

03:23.160 --> 03:24.990
And let's make a method.

03:25.920 --> 03:28.800
Private void handle.

03:28.800 --> 03:29.550
Lip.

03:30.200 --> 03:36.590
And inside of this method, we're going to make an if statement that will check if our velocity dot

03:36.590 --> 03:45.470
x is less than zero means we're moving to the left and he is facing right and facing right, then we

03:45.470 --> 03:47.900
need to add a flip method over here.

03:47.900 --> 03:49.310
So he's going to flip like that.

03:49.310 --> 03:52.820
Now let's go and check if he is flipping when he ran into the left.

03:52.820 --> 03:55.250
And we just need to call this method in the update.

03:55.520 --> 04:00.170
So over here after the movement I'm going to do handle flip.

04:01.800 --> 04:04.290
Okay, let's save it and let's go back.

04:11.570 --> 04:12.830
Now I'm standing.

04:12.830 --> 04:16.010
I'm going to run to the left and we flip in.

04:16.160 --> 04:19.160
But it is sort of too much.

04:19.160 --> 04:19.670
It's okay.

04:19.670 --> 04:21.020
We're going to fix it anyway.

04:21.020 --> 04:21.980
Now he flipped.

04:22.640 --> 04:26.150
And when I'm trying to run to the right, he's not flipping.

04:26.800 --> 04:27.250
Right.

04:27.250 --> 04:28.960
So how are we going to fix it?

04:31.440 --> 04:37.290
First of all, when we flip, we need not only to flip the character, but we need to say that he is

04:37.290 --> 04:38.940
not facing right anymore.

04:39.120 --> 04:43.200
And we could do it by saying facing right equals to false.

04:43.290 --> 04:46.020
Over here in the curly brackets.

04:46.690 --> 04:52.390
But that would be sort of an extra job because you can set Boolean to an opposite value in a different

04:52.390 --> 04:52.930
way.

04:53.290 --> 04:55.540
Let me just bring it back to what it was.

04:56.640 --> 05:04.440
And over here, every time we call a method flip, we're going to say facing right equals to not facing

05:04.440 --> 05:04.920
right.

05:04.920 --> 05:07.980
Exclamation mark over here means not.

05:07.980 --> 05:12.510
And this one basically says if you are true, become false.

05:12.510 --> 05:14.670
If you are false, become true.

05:14.850 --> 05:17.790
Just take an opposite value from what you have.

05:17.820 --> 05:21.810
Now let's go again to unity and see how it's working.

05:28.720 --> 05:32.650
And I'm standing there, I'm going to run to the left and I'm flipping only once.

05:32.650 --> 05:36.790
But when I try to run to the right, there is no flip.

05:36.790 --> 05:39.520
So we need to add flip for the right side as well.

05:42.180 --> 05:47.160
We can do that by adding here an Or condition.

05:47.160 --> 05:54.000
So if you type two slashes like that then it means or it's like typing or.

05:54.030 --> 05:54.810
Right.

05:55.170 --> 05:59.430
And we're doing one check or if.

06:00.650 --> 06:09.290
RB velocity dot x is bigger than zero, which means we're moving to the right and not facing right means

06:09.290 --> 06:10.940
we need to face the right side.

06:10.940 --> 06:13.190
And that's why we need to flip again.

06:13.190 --> 06:15.350
So let's go ahead and see if it's working.

06:19.250 --> 06:21.050
And we're good to go.

06:21.080 --> 06:23.540
We're only going to need to add one more update here.

06:23.540 --> 06:24.950
One more improvement.

06:26.540 --> 06:33.230
So I'm running to the right, left, right, left and it even works with the jump.

06:34.450 --> 06:35.290
Yay!

06:36.770 --> 06:37.850
Very cool rate.

06:37.850 --> 06:39.200
I love it so much.

06:41.830 --> 06:44.350
So basically we've got a platformer here.

06:44.350 --> 06:50.950
You just take a platform, uh, place it here, and now you have a platformer where you need to jump.

06:51.280 --> 06:52.090
Very cool.

06:52.720 --> 06:57.190
Now let's go to player and add one variable over here.

06:58.360 --> 07:03.940
It's going to be integer private int facing direction.

07:04.120 --> 07:05.170
It's going to be equals to one.

07:05.170 --> 07:05.830
By default.

07:05.830 --> 07:08.290
We're going to need this later on for different purposes.

07:08.290 --> 07:13.090
And this one will just help us to know when character is facing right or left.

07:13.300 --> 07:18.760
And we need it as a number because we're going to use it to move character to different sides, for

07:18.760 --> 07:23.380
example for knockback or for the wall jump or anything like that.

07:23.380 --> 07:25.630
So let's go back to the.

07:26.940 --> 07:28.980
Flip over here and type.

07:29.940 --> 07:36.720
Facing direction equals to facing direction multiplied by minus one, and this will multiply value by

07:36.720 --> 07:38.490
minus one every time we flip it.

07:38.490 --> 07:40.770
So it will be 1 or -1.

07:40.770 --> 07:43.470
Let's go and see how it works in a debug mode.

07:48.310 --> 07:52.570
So here debug we can see facing direction is equals to one.

07:52.960 --> 07:57.610
I'm facing left minus one one minus one one minus one.

07:58.030 --> 07:58.960
Very cool right.

07:59.440 --> 08:00.310
Yeah I think so.

08:00.310 --> 08:01.690
Well great job.

08:01.690 --> 08:02.920
Thank you for paying attention.

08:02.920 --> 08:04.270
I'll see you in the next video.
