Hello guys.
Welcome to the video.
In this one I want to make a wall jump ability for the player.
Before we do that, I need to explain something to you so you are ready to use it.
And I'm talking about coroutines.
I have a great video that explains coroutines and how to do other things with time in unity.
I will attach that video to this lecture so you can check that out and, you know, extend your knowledge.
But also I'm going to explain it right now.
So what is a coroutine?
How to use it?
Why do we need it.
Let's go to the player.
Over here.
And just here I'll type private I enumerator.
It's not available.
It's available.
Okay, I enumerator and let's give it a name of.
Core routine.
Now, coroutine is just like a method, but you can pause the functionality of this method.
You can pause the coroutine, you can ask it to wait for some seconds or something like that and do
the functionality you need.
Example.
Let's do type here Debug.log.
Coroutine started.
Then I'm going to yield.
Return new weight four seconds one.
Then let's do debug log.
One second past.
Then let's do.
Yield.
Return.
New.
Wait for seconds.
Three.
And I'm going to type debug log.
Now.
Quarantine is over.
So by the logic of this coroutine, we can see that we're going to debug a message.
We're going to wait for one second.
We're going to say that one second passed.
Then we're going to wait another three seconds.
Then we're going to say, now coroutine is over.
And to start the coroutine, you can not just type a name of a method like that, it will not work.
You actually need to type start coroutine, then give name of a coroutine.
Okay, let's go ahead and see how it's going to work because we're going to need this in this video.
I'm going to open console.
Go to play mode.
And we can see.
Coroutine started one second passed.
Now coroutine is over.
Very useful thing for us.
We're going to use a couple of times, and it's going to be important in this video as well.
Now our goal is to make a wall jump, but at the moment our system will not allow us to do wall jump
because.
Uh, over here in the handle movement, we update movement of the character with the X input.
If you have a zero input, your character will stand and that will happen even if you try to jump off
the wall.
Basically, this controls the entire movement, and if you try to do some other movement, it will not
work because we interrupting and we updating x velocity at all times.
And because of that, this will always have a higher priority unless we switch it off.
So when we're doing a wall jump, we need to do wall jump.
And we need to switch off the horizontal movement for some time.
And that is why we need a coroutine.
So in this video we're going to have a couple of new things that you did not see before.
Maybe, but brace yourself.
We are here to learn and it's a fun stuff to do okay.
Let's type here.
Header.
And I'm going to type wall interactions.
And I think it's a good idea to remove details from here.
Actually, I think, yeah, an info from here.
I just feel like it.
I'm sorry guys.
Now for the wall interactions.
First of all, we need a serialized field.
Private.
Float wall jump duration.
We're going to use this variable to pause the x input for the movement of the character.
So this value will define how long we cannot affect the character.
And I'm going to make it equals 2.65 by default maybe 0.6.
By the way this is how you set default value.
And then you're going to have this value in the inspector.
And you won't have to add this manually.
Then let's do serialize field private Vector2.
And for the wall jump when we jump in of the wall.
We need to know.
Y value and x value, because we want to know how high we want to jump and also how far we want to jump.
For that, we need to know two values x and y.
And that's why we use a vector two over here.
And I'm going to call it wall jump force.
And we need another boolean here.
Private bool is wall jumping.
All right.
We have all of the variables we need to make a wall jump.
Now let's go somewhere to the jump functions over here and we can make another method.
Private void wall jump.
Inside the wall jump.
We're going to apply our velocity equals to new vector two.
And here we're going to need wall jump force dot x.
Multiply it by negative facing direction.
Because when we are on the wall let me show it to you real quick if it will work now.
It will not work because I have a cold here.
Let me comment this out.
So when we are on the wall, even though sprite looks to the left, the actual facing direction is to
the right.
We can see that by gizmos over here.
And also if we open debug we can see our facing direction is one over here.
So when we do a wall jump we need to jump to an opposite direction.
And for that we just need to type wall jump force X multiplied by negative facing direction, which
is basically an opposite to a facing direction.
We have.
And then we'll jump force Y.
And also when we jump we want to say is wall jump in equals to true.
To let us know that we do in a wall jump.
And while we're doing a wall jump, we need to stop movement of the character.
But before we do that, let's actually go ahead and try that now without stopping the movement.
I want you to see what is going to happen.
We need to go here to the wall, jump force and make x five and y 15.
And I'm going to show you why we're doing that in a second.
And also I'm going to tell you how to adjust these values for you.
As of now let's just see how it's going to work.
Okay.
So because I want you to learn the process of the making as well, how did they come up to this?
And just to let you know, it's not like I magically know all of this.
It took me quite a while to develop this controller, and there are lots of bugs and messy behaviors.
So yeah, this is how I came up with this.
So when we jump on the wall and we're trying to jump, he's just going up.
Right?
Well, first of all, because I do not call function wall jump.
I'm so I'm so silly.
Sometimes we have wall jump, but we did not use it.
Let's go to the jump button and make else if is well detected.
Are we doing this?
Like that.
We'll detect it.
Then do a wall jump and it's important to do it before the double jump.
Make sure you're doing it that way.
So if we'll detect it, we're going to do we'll jump.
I'm doing a wall jump and you can see what happened.
Although I was moved to the left just a little bit, my movement was interrupted by the X input.
That is zero at the moment, and when we have x input zero, the velocity on the x is also zero and
we're not moving at all.
So we can go to handle movement over here and do this.
If is wall jumping.
Then return.
Okay.
And you see what I was talking about when I said, it's better to do return.
It's a bit cleaner, right?
It's a bit easier to understand.
Well, detected return will jump in return instead of seeing nested if statements.
Okay, let's go back and try to do wall jump again.
Going to jump on the wall and wall jump.
Yay!
Works very well.
I love it so much.
Now we cannot move because we saying that if is wall jumping, you cannot move and we never set wall
jump into falls.
We can set wall jump into falls with the help of a coroutine that I was talking about in the beginning
of this video, just below the wall jump.
Let's do private I enumerator wall jump routine.
And over here we're going to do is wall jump in false yield return new waitforseconds of wall jump duration
and then is wall jump in.
True.
Wait a second.
Should be in opposite.
We set it to true.
We wait, we set it to false.
Okay, now we don't need to do it here.
We can just go here and start.
Coroutine of wall jump routine.
Very, very good.
Let's save this and let's see what we've got now.
I'm going to do wall jump and wall jump in and I can move again.
Super cool.
We still have a couple of things to do to make it work better, but as of now, even as of now, it's
working.
A couple of important things here before we apply other fixes.
Belgium duration should have some value.
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
going to be interrupted just like you see over here.
If you say 0.25, it's still not enough.
So we're supposed to have at least 0.5, 0.6 here, maybe even 0.7.
I'm going to use 0.6.
This is enough to do the wall jump.
And at the same time it's enough.
You know, like their player will not do any input for sure for the point six second because he's expecting
for wall jump to work.
So he will not interrupt the movement.
Anyhow.
Uh, you can adjust the power of wall jump by changing these values.
X basically says the horizontal jump, y the vertical jump.
If you increase the x, your jump is going to be more horizontal.
If you lower the y, it's going to be less vertical.
So you can do it like that, or you can do it like.
That.
Which is not really what we need, and also something important.
If you decide to make a high wall jump, maybe in some cases you need that.
You can see that movement was interrupted during the wall jump.
So maybe you need to increase wall jump duration then something like 0.8.
And this is cool, right?
I love it so much.
Now for me, I'm going to type here maybe seven and 14.
I want it to be a bit more vertical and jump duration going to be 0.6 for now.
I think I'm happy with it and if we need, we can always adjust.
Another thing you've seen that when he's jumping, he's jumping with his back and we want him to face
the direction he is moving to, so he is ready for the next wall.
Uh, let's just fix it by typing over here.
Philip.
Yeah.
Just that maybe after the velocity, actually we apply velocity, then we flip and start a coroutine
and let's go back.
And jump.
Jump!
And now we have a wall jump.
Nice.
And you cannot interrupt the movement of the wall jump, but you can flip.
And for that, I guess player will have to learn not to press buttons while he doing wall jump, because
then he'll be facing another direction.
Um, although if you like, you can just make a private boolean, something like can flip, which actually
we're going to need later on.
And you can do something like can flip false and then.
Can flip through, then your character will not be able to flip during the wall jump.
I want to use the double jump during the wall jump for the cases when player wants to change direction.
I really like how it feels.
So we're going to go to the double jump and over here we're going to type is wall jumping equals to
false.
All right.
So this will interrupt the wall jump if you're trying to do double jump.
And because of that you're going to have more flexible movement.
Look at this.
I can go on the wall.
I can jump and do a double jump to change my direction.
And it feels very, you know, fluent, I would say.
And the thing is, if you do double jump once and then you not land on the ground.
Uh, just let me try.
You cannot do double jump again, because we reset and double jump only when we touch the ground.
So I guess it's better to do reset of a double jump in double jump as well, just so we are ready for
the next time.
So in a wall jump over here we can type can double jump.
True.
And lastly, just to be sure, we cannot double jump while we stand on the ground.
Let's go here and add and not is grounded because we'll jump.
Should be available only when you are not grounded in your on the wall.
Well basically that's it.
Wall jump is ready.
I really like how it works and we don't need to adjust anything else.
I believe if something comes up I'll let you know.
Oh what was that glitch?
Did you see the glitch?
All right, so I found how to fix the bug.
This is what is happening.
We starting a coroutine.
Every time we press a ball jump, this coroutine starts as well.
Jumping.
True.
Then it makes it false after some time.
If you do wall jumps really quick, then you can start coroutine one after another, and by the time
you're doing second wall jump, it may happen.
That first coroutine from a first wall jump may cancel the second wall jump that you started right after,
because it happened all so quick, and your second wall jump is still happening during the wall jump
duration of the first jump and you just have this is wall jumping being canceled from a first wall jump.
All right.
I hope that makes sense.
Anyway, we're going to fix it right now.
It's not that difficult.
So what we can do here is first we go to Wall Jump and we're going to stop all coroutines.
You should know that this method will stop all coroutines that were started in this script.
So if you plan to do any other functionality with the coroutine inside of this script, be aware that
you may cancel that coroutine with this line of code over here.
And that's all what we need to do.
Also, I came back to this video to fix the bug.
So if you see your wall jump function and double jump function written a bit differently, don't worry,
this is how it's supposed to be.
Okay.
All right, I'll see you in the next video.
Bye.