WEBVTT

00:00.020 --> 00:00.440
Hello guys.

00:00.440 --> 00:01.370
Welcome to the video.

00:01.370 --> 00:04.670
In this one I want to make a wall jump ability for the player.

00:04.670 --> 00:08.570
Before we do that, I need to explain something to you so you are ready to use it.

00:08.570 --> 00:10.460
And I'm talking about coroutines.

00:10.460 --> 00:16.460
I have a great video that explains coroutines and how to do other things with time in unity.

00:16.460 --> 00:20.600
I will attach that video to this lecture so you can check that out and, you know, extend your knowledge.

00:20.600 --> 00:22.280
But also I'm going to explain it right now.

00:22.280 --> 00:24.020
So what is a coroutine?

00:24.020 --> 00:24.860
How to use it?

00:24.860 --> 00:25.790
Why do we need it.

00:25.790 --> 00:27.290
Let's go to the player.

00:28.690 --> 00:29.710
Over here.

00:29.710 --> 00:34.540
And just here I'll type private I enumerator.

00:36.470 --> 00:37.940
It's not available.

00:38.180 --> 00:38.690
It's available.

00:38.690 --> 00:43.130
Okay, I enumerator and let's give it a name of.

00:43.980 --> 00:45.570
Core routine.

00:48.090 --> 00:55.200
Now, coroutine is just like a method, but you can pause the functionality of this method.

00:55.200 --> 01:01.200
You can pause the coroutine, you can ask it to wait for some seconds or something like that and do

01:01.200 --> 01:02.580
the functionality you need.

01:02.610 --> 01:03.300
Example.

01:03.300 --> 01:06.270
Let's do type here Debug.log.

01:07.240 --> 01:09.070
Coroutine started.

01:09.970 --> 01:11.380
Then I'm going to yield.

01:12.300 --> 01:17.310
Return new weight four seconds one.

01:17.340 --> 01:19.140
Then let's do debug log.

01:19.840 --> 01:22.270
One second past.

01:23.390 --> 01:24.410
Then let's do.

01:24.440 --> 01:24.770
Yield.

01:24.770 --> 01:25.070
Return.

01:25.070 --> 01:25.340
New.

01:25.370 --> 01:26.360
Wait for seconds.

01:26.360 --> 01:27.290
Three.

01:27.410 --> 01:30.110
And I'm going to type debug log.

01:30.290 --> 01:31.160
Now.

01:31.900 --> 01:33.490
Quarantine is over.

01:34.740 --> 01:39.210
So by the logic of this coroutine, we can see that we're going to debug a message.

01:39.210 --> 01:40.800
We're going to wait for one second.

01:40.800 --> 01:42.870
We're going to say that one second passed.

01:43.200 --> 01:45.210
Then we're going to wait another three seconds.

01:45.210 --> 01:48.390
Then we're going to say, now coroutine is over.

01:48.780 --> 01:54.420
And to start the coroutine, you can not just type a name of a method like that, it will not work.

01:54.420 --> 01:59.460
You actually need to type start coroutine, then give name of a coroutine.

02:01.590 --> 02:08.340
Okay, let's go ahead and see how it's going to work because we're going to need this in this video.

02:14.290 --> 02:15.640
I'm going to open console.

02:15.940 --> 02:17.080
Go to play mode.

02:17.990 --> 02:19.070
And we can see.

02:19.070 --> 02:21.410
Coroutine started one second passed.

02:23.190 --> 02:24.690
Now coroutine is over.

02:26.280 --> 02:28.080
Very useful thing for us.

02:29.230 --> 02:33.160
We're going to use a couple of times, and it's going to be important in this video as well.

02:33.160 --> 02:40.360
Now our goal is to make a wall jump, but at the moment our system will not allow us to do wall jump

02:40.360 --> 02:41.680
because.

02:42.410 --> 02:49.370
Uh, over here in the handle movement, we update movement of the character with the X input.

02:49.370 --> 02:55.190
If you have a zero input, your character will stand and that will happen even if you try to jump off

02:55.190 --> 02:55.820
the wall.

02:55.850 --> 03:02.210
Basically, this controls the entire movement, and if you try to do some other movement, it will not

03:02.210 --> 03:07.130
work because we interrupting and we updating x velocity at all times.

03:07.130 --> 03:12.620
And because of that, this will always have a higher priority unless we switch it off.

03:12.620 --> 03:15.920
So when we're doing a wall jump, we need to do wall jump.

03:15.920 --> 03:19.370
And we need to switch off the horizontal movement for some time.

03:19.370 --> 03:21.440
And that is why we need a coroutine.

03:21.440 --> 03:25.580
So in this video we're going to have a couple of new things that you did not see before.

03:25.580 --> 03:27.080
Maybe, but brace yourself.

03:27.080 --> 03:29.930
We are here to learn and it's a fun stuff to do okay.

03:30.680 --> 03:31.790
Let's type here.

03:31.790 --> 03:32.480
Header.

03:33.390 --> 03:36.180
And I'm going to type wall interactions.

03:37.130 --> 03:40.310
And I think it's a good idea to remove details from here.

03:40.310 --> 03:42.830
Actually, I think, yeah, an info from here.

03:42.830 --> 03:43.700
I just feel like it.

03:43.700 --> 03:44.600
I'm sorry guys.

03:44.600 --> 03:46.550
Now for the wall interactions.

03:46.550 --> 03:49.580
First of all, we need a serialized field.

03:50.090 --> 03:50.870
Private.

03:51.550 --> 03:55.030
Float wall jump duration.

03:55.030 --> 04:00.190
We're going to use this variable to pause the x input for the movement of the character.

04:00.190 --> 04:04.840
So this value will define how long we cannot affect the character.

04:04.840 --> 04:08.980
And I'm going to make it equals 2.65 by default maybe 0.6.

04:08.980 --> 04:10.960
By the way this is how you set default value.

04:10.960 --> 04:12.820
And then you're going to have this value in the inspector.

04:12.820 --> 04:14.830
And you won't have to add this manually.

04:15.400 --> 04:18.580
Then let's do serialize field private Vector2.

04:18.580 --> 04:22.390
And for the wall jump when we jump in of the wall.

04:23.400 --> 04:24.660
We need to know.

04:25.440 --> 04:33.180
Y value and x value, because we want to know how high we want to jump and also how far we want to jump.

04:33.210 --> 04:36.330
For that, we need to know two values x and y.

04:36.330 --> 04:38.610
And that's why we use a vector two over here.

04:38.610 --> 04:41.790
And I'm going to call it wall jump force.

04:42.610 --> 04:44.800
And we need another boolean here.

04:44.800 --> 04:47.980
Private bool is wall jumping.

04:48.940 --> 04:49.510
All right.

04:49.510 --> 04:52.420
We have all of the variables we need to make a wall jump.

04:52.660 --> 04:59.290
Now let's go somewhere to the jump functions over here and we can make another method.

04:59.620 --> 05:02.170
Private void wall jump.

05:03.030 --> 05:04.290
Inside the wall jump.

05:04.290 --> 05:09.390
We're going to apply our velocity equals to new vector two.

05:09.390 --> 05:13.320
And here we're going to need wall jump force dot x.

05:13.320 --> 05:17.280
Multiply it by negative facing direction.

05:17.280 --> 05:22.350
Because when we are on the wall let me show it to you real quick if it will work now.

05:25.550 --> 05:27.410
It will not work because I have a cold here.

05:27.410 --> 05:28.700
Let me comment this out.

05:35.660 --> 05:41.720
So when we are on the wall, even though sprite looks to the left, the actual facing direction is to

05:41.720 --> 05:42.230
the right.

05:42.230 --> 05:44.240
We can see that by gizmos over here.

05:44.240 --> 05:49.550
And also if we open debug we can see our facing direction is one over here.

05:49.550 --> 05:53.180
So when we do a wall jump we need to jump to an opposite direction.

05:53.180 --> 05:59.930
And for that we just need to type wall jump force X multiplied by negative facing direction, which

05:59.930 --> 06:01.970
is basically an opposite to a facing direction.

06:01.970 --> 06:02.600
We have.

06:02.600 --> 06:04.670
And then we'll jump force Y.

06:04.670 --> 06:09.350
And also when we jump we want to say is wall jump in equals to true.

06:09.380 --> 06:11.810
To let us know that we do in a wall jump.

06:11.810 --> 06:18.020
And while we're doing a wall jump, we need to stop movement of the character.

06:18.020 --> 06:22.760
But before we do that, let's actually go ahead and try that now without stopping the movement.

06:22.760 --> 06:24.530
I want you to see what is going to happen.

06:24.530 --> 06:30.350
We need to go here to the wall, jump force and make x five and y 15.

06:30.350 --> 06:33.650
And I'm going to show you why we're doing that in a second.

06:33.650 --> 06:36.140
And also I'm going to tell you how to adjust these values for you.

06:36.500 --> 06:38.990
As of now let's just see how it's going to work.

06:39.840 --> 06:40.350
Okay.

06:40.350 --> 06:45.210
So because I want you to learn the process of the making as well, how did they come up to this?

06:45.210 --> 06:48.660
And just to let you know, it's not like I magically know all of this.

06:48.690 --> 06:54.270
It took me quite a while to develop this controller, and there are lots of bugs and messy behaviors.

06:54.270 --> 06:57.240
So yeah, this is how I came up with this.

06:57.880 --> 07:02.230
So when we jump on the wall and we're trying to jump, he's just going up.

07:02.940 --> 07:03.750
Right?

07:03.960 --> 07:06.690
Well, first of all, because I do not call function wall jump.

07:07.410 --> 07:09.300
I'm so I'm so silly.

07:09.300 --> 07:12.330
Sometimes we have wall jump, but we did not use it.

07:12.330 --> 07:17.610
Let's go to the jump button and make else if is well detected.

07:18.420 --> 07:19.440
Are we doing this?

07:20.150 --> 07:20.750
Like that.

07:21.840 --> 07:22.560
We'll detect it.

07:22.560 --> 07:26.640
Then do a wall jump and it's important to do it before the double jump.

07:27.090 --> 07:29.010
Make sure you're doing it that way.

07:29.670 --> 07:32.880
So if we'll detect it, we're going to do we'll jump.

07:38.030 --> 07:40.850
I'm doing a wall jump and you can see what happened.

07:40.850 --> 07:47.240
Although I was moved to the left just a little bit, my movement was interrupted by the X input.

07:47.330 --> 07:53.240
That is zero at the moment, and when we have x input zero, the velocity on the x is also zero and

07:53.240 --> 07:54.440
we're not moving at all.

07:54.440 --> 07:58.010
So we can go to handle movement over here and do this.

07:58.010 --> 07:59.870
If is wall jumping.

08:00.750 --> 08:02.010
Then return.

08:02.790 --> 08:03.390
Okay.

08:04.200 --> 08:07.440
And you see what I was talking about when I said, it's better to do return.

08:07.440 --> 08:08.550
It's a bit cleaner, right?

08:08.550 --> 08:09.930
It's a bit easier to understand.

08:09.930 --> 08:14.730
Well, detected return will jump in return instead of seeing nested if statements.

08:15.150 --> 08:18.210
Okay, let's go back and try to do wall jump again.

08:20.990 --> 08:23.480
Going to jump on the wall and wall jump.

08:23.510 --> 08:24.350
Yay!

08:24.350 --> 08:25.700
Works very well.

08:25.700 --> 08:26.720
I love it so much.

08:26.720 --> 08:32.630
Now we cannot move because we saying that if is wall jumping, you cannot move and we never set wall

08:32.630 --> 08:34.010
jump into falls.

08:34.610 --> 08:39.380
We can set wall jump into falls with the help of a coroutine that I was talking about in the beginning

08:39.380 --> 08:42.170
of this video, just below the wall jump.

08:42.170 --> 08:47.630
Let's do private I enumerator wall jump routine.

08:47.840 --> 08:58.940
And over here we're going to do is wall jump in false yield return new waitforseconds of wall jump duration

08:59.540 --> 09:01.700
and then is wall jump in.

09:02.310 --> 09:02.850
True.

09:02.880 --> 09:03.720
Wait a second.

09:03.720 --> 09:04.950
Should be in opposite.

09:05.100 --> 09:06.540
We set it to true.

09:07.390 --> 09:09.580
We wait, we set it to false.

09:10.610 --> 09:13.160
Okay, now we don't need to do it here.

09:13.160 --> 09:15.320
We can just go here and start.

09:15.890 --> 09:18.770
Coroutine of wall jump routine.

09:19.800 --> 09:21.060
Very, very good.

09:21.060 --> 09:23.580
Let's save this and let's see what we've got now.

09:28.570 --> 09:33.220
I'm going to do wall jump and wall jump in and I can move again.

09:34.080 --> 09:35.250
Super cool.

09:35.250 --> 09:40.530
We still have a couple of things to do to make it work better, but as of now, even as of now, it's

09:40.530 --> 09:41.160
working.

09:41.160 --> 09:44.550
A couple of important things here before we apply other fixes.

09:44.760 --> 09:46.890
Belgium duration should have some value.

09:46.890 --> 09:51.510
If you put a low value here, it's going to be like you don't have it and because of that you will jump

09:51.510 --> 09:53.940
going to be interrupted just like you see over here.

09:54.240 --> 09:57.390
If you say 0.25, it's still not enough.

09:57.390 --> 10:02.220
So we're supposed to have at least 0.5, 0.6 here, maybe even 0.7.

10:02.220 --> 10:03.450
I'm going to use 0.6.

10:03.750 --> 10:06.240
This is enough to do the wall jump.

10:06.240 --> 10:08.760
And at the same time it's enough.

10:08.760 --> 10:14.760
You know, like their player will not do any input for sure for the point six second because he's expecting

10:14.760 --> 10:15.810
for wall jump to work.

10:15.810 --> 10:17.910
So he will not interrupt the movement.

10:17.910 --> 10:18.900
Anyhow.

10:19.410 --> 10:23.970
Uh, you can adjust the power of wall jump by changing these values.

10:24.270 --> 10:28.920
X basically says the horizontal jump, y the vertical jump.

10:28.920 --> 10:33.660
If you increase the x, your jump is going to be more horizontal.

10:33.660 --> 10:36.690
If you lower the y, it's going to be less vertical.

10:37.970 --> 10:40.400
So you can do it like that, or you can do it like.

10:40.880 --> 10:41.750
That.

10:42.300 --> 10:46.350
Which is not really what we need, and also something important.

10:46.350 --> 10:50.130
If you decide to make a high wall jump, maybe in some cases you need that.

10:50.130 --> 10:52.920
You can see that movement was interrupted during the wall jump.

10:52.920 --> 10:57.690
So maybe you need to increase wall jump duration then something like 0.8.

10:59.230 --> 11:01.000
And this is cool, right?

11:01.000 --> 11:01.990
I love it so much.

11:01.990 --> 11:07.720
Now for me, I'm going to type here maybe seven and 14.

11:07.720 --> 11:12.670
I want it to be a bit more vertical and jump duration going to be 0.6 for now.

11:12.760 --> 11:16.810
I think I'm happy with it and if we need, we can always adjust.

11:17.260 --> 11:23.650
Another thing you've seen that when he's jumping, he's jumping with his back and we want him to face

11:23.650 --> 11:28.120
the direction he is moving to, so he is ready for the next wall.

11:28.690 --> 11:31.420
Uh, let's just fix it by typing over here.

11:32.560 --> 11:33.190
Philip.

11:34.630 --> 11:35.230
Yeah.

11:35.230 --> 11:40.930
Just that maybe after the velocity, actually we apply velocity, then we flip and start a coroutine

11:41.260 --> 11:42.670
and let's go back.

11:47.380 --> 11:49.330
And jump.

11:49.780 --> 11:50.260
Jump!

11:50.260 --> 11:52.000
And now we have a wall jump.

11:52.640 --> 11:53.630
Nice.

11:54.420 --> 11:58.230
And you cannot interrupt the movement of the wall jump, but you can flip.

11:58.230 --> 12:03.600
And for that, I guess player will have to learn not to press buttons while he doing wall jump, because

12:03.600 --> 12:06.060
then he'll be facing another direction.

12:06.830 --> 12:13.880
Um, although if you like, you can just make a private boolean, something like can flip, which actually

12:13.880 --> 12:15.290
we're going to need later on.

12:17.270 --> 12:21.410
And you can do something like can flip false and then.

12:22.220 --> 12:26.750
Can flip through, then your character will not be able to flip during the wall jump.

12:28.160 --> 12:35.030
I want to use the double jump during the wall jump for the cases when player wants to change direction.

12:35.030 --> 12:36.590
I really like how it feels.

12:36.590 --> 12:43.670
So we're going to go to the double jump and over here we're going to type is wall jumping equals to

12:43.670 --> 12:44.330
false.

12:44.840 --> 12:45.530
All right.

12:46.260 --> 12:49.410
So this will interrupt the wall jump if you're trying to do double jump.

12:49.410 --> 12:52.590
And because of that you're going to have more flexible movement.

12:53.440 --> 12:54.280
Look at this.

12:54.280 --> 12:55.630
I can go on the wall.

12:55.630 --> 12:58.180
I can jump and do a double jump to change my direction.

12:58.180 --> 13:01.450
And it feels very, you know, fluent, I would say.

13:01.840 --> 13:06.370
And the thing is, if you do double jump once and then you not land on the ground.

13:08.060 --> 13:09.260
Uh, just let me try.

13:11.970 --> 13:16.290
You cannot do double jump again, because we reset and double jump only when we touch the ground.

13:16.290 --> 13:22.440
So I guess it's better to do reset of a double jump in double jump as well, just so we are ready for

13:22.440 --> 13:23.100
the next time.

13:23.100 --> 13:26.070
So in a wall jump over here we can type can double jump.

13:26.100 --> 13:26.700
True.

13:27.890 --> 13:32.690
And lastly, just to be sure, we cannot double jump while we stand on the ground.

13:33.780 --> 13:38.400
Let's go here and add and not is grounded because we'll jump.

13:38.400 --> 13:42.600
Should be available only when you are not grounded in your on the wall.

13:42.600 --> 13:45.780
Well basically that's it.

13:45.780 --> 13:47.160
Wall jump is ready.

13:47.160 --> 13:51.030
I really like how it works and we don't need to adjust anything else.

13:51.030 --> 13:53.280
I believe if something comes up I'll let you know.

13:53.490 --> 13:55.080
Oh what was that glitch?

13:55.080 --> 13:56.280
Did you see the glitch?

13:56.280 --> 13:58.080
All right, so I found how to fix the bug.

13:58.080 --> 13:59.340
This is what is happening.

13:59.340 --> 14:00.930
We starting a coroutine.

14:00.930 --> 14:04.800
Every time we press a ball jump, this coroutine starts as well.

14:04.800 --> 14:05.190
Jumping.

14:05.190 --> 14:05.640
True.

14:05.670 --> 14:08.010
Then it makes it false after some time.

14:08.010 --> 14:13.620
If you do wall jumps really quick, then you can start coroutine one after another, and by the time

14:13.620 --> 14:15.840
you're doing second wall jump, it may happen.

14:15.840 --> 14:22.920
That first coroutine from a first wall jump may cancel the second wall jump that you started right after,

14:22.920 --> 14:27.690
because it happened all so quick, and your second wall jump is still happening during the wall jump

14:27.690 --> 14:34.650
duration of the first jump and you just have this is wall jumping being canceled from a first wall jump.

14:34.650 --> 14:35.070
All right.

14:35.070 --> 14:36.150
I hope that makes sense.

14:36.150 --> 14:37.560
Anyway, we're going to fix it right now.

14:37.560 --> 14:38.580
It's not that difficult.

14:38.580 --> 14:44.430
So what we can do here is first we go to Wall Jump and we're going to stop all coroutines.

14:44.430 --> 14:50.280
You should know that this method will stop all coroutines that were started in this script.

14:50.280 --> 14:55.020
So if you plan to do any other functionality with the coroutine inside of this script, be aware that

14:55.020 --> 14:58.830
you may cancel that coroutine with this line of code over here.

14:58.830 --> 15:00.240
And that's all what we need to do.

15:00.240 --> 15:03.300
Also, I came back to this video to fix the bug.

15:03.300 --> 15:10.260
So if you see your wall jump function and double jump function written a bit differently, don't worry,

15:10.260 --> 15:11.820
this is how it's supposed to be.

15:11.820 --> 15:12.210
Okay.

15:12.210 --> 15:14.160
All right, I'll see you in the next video.

15:14.160 --> 15:14.430
Bye.
