Hello guys.
Welcome to the video.
In this one we're going to make a wall slide ability.
And then one after that we're going to make a wall jump.
But first let's do a wall slide to make a wall slide.
First of all we need some walls.
So I'm going to duplicate this platform move it here and then stretch it with the rack tool to make
it something like this.
Yeah.
And then let me make another one.
And place it here.
Maybe.
Yeah.
Something like this.
Supposed to work, I guess.
Now, if I try to jump on the wall right now, I'm not going to slide, especially if I'm holding the
key to fix it.
We need to add a physics material to the player.
We can click right button over here, create 2D physics material 2D and let's call it player material.
Okay.
Now friction should be zero here.
Let's make it a zero.
And then let's select the player and drag player material on the capsule Collider like that.
Very good.
Now we can go to play mode.
And I'm just going to try to slide again.
And I'm sliding.
Very nice.
Now next step would be to detect the wall and enter some sort of wall slide behavior.
For example player supposed to slide a bit slower than he does it right now.
For that, first of all we need to make a wall detection.
Let's select player script.
Then on the collision over here, we can make another private boolean.
Private wall is wall detected and I think we need wall check distance.
So I'm going to make it over here.
Sterilize field private float wall check distance.
Just like that.
Let's go to handle collision method.
We can click go to definition.
And over here we need to make Esbl detected equals to physics 2D dot raycast.
And last time we did raycast down.
But now we need to do it to the right.
So transform position vector two dot right multiplied by facing direction.
Because we want to flip raycast together with a character, right?
Because otherwise if we don't do this then raycast will work only to the right side.
Then let's type wall check distance and what is ground.
Just like that.
Very nice.
Let's save it.
And let's go to gizmos and draw a line so we can see it.
Gizmos draw a line from transform position to new vector two.
And here we need transform position.
Plus let's open parentheses and type wall check distance multiplied by facing direction.
So again we're going to flip the gizmo as well.
And for the Y we just need.
Transform position dot y.
Uh, give me a second.
Something is wrong.
Oh.
Over here, dot x.
Okay, so this is how it looks.
Now let's just save it and go to unity and make sure it is working correctly.
We're going to choose.
We're going to set wall check distance to something like zero point.
65.
Let me try to see it.
Oh, perfect.
So this is the wall check distance, and we just need to set it to something like this.
So 65 is just a good value.
Now let's go to play mode and let's open debug mode so we can see we'll.
Detected Boolean over here is well detected.
I'm going to walk up to the wall and we'll detect it.
Or in a way not detected.
Detected not okay.
Now let's make a wall slide behavior.
We're going to open player.
And maybe somewhere before we handle the movement.
Let's do handle.
Handle.
Wall slide.
Now we need to click alt enter and generate method handle.
Wall.
Slide.
Nice.
Now over here we just need to check if wall detected.
Means we detected the wall.
This is good.
And if we move it down because we want to slow down the player only when he like falling down.
Right.
So we're going to do.
And RB velocity dot y less than zero.
Then we will slide in.
So let's just do debug log.
We'll slide in now.
For now.
Let's just see how it's working.
I'm going to go back.
Mm.
Open console, go to play mode.
And I'm going to stand next to the wall.
No debug message.
I'm going to jump.
No debug message, and only when I'm falling there is a debug message.
So this is good.
Now let's try to slow down the character.
I'm going to do RB velocity equals to a new vector two for the x.
We're not going to change anything.
So rb velocity dot x.
And for the y we're going to take rb dot velocity dot y.
And we're going to multiply it by 0.5 f.
So character is going to slide down slower.
This one basically is like um taking 50% of the velocity that we currently have.
I'm going to save it.
Let's go to play mode and let's try to jump on the wall and we slowly sliding down.
Very nice.
We're going to improve it a bit, but later on as of now let's just add animation so it looks good.
Uh, on the animation we're going to select animator and add new Clip.
Over here you're going to make layer wall slide.
Let's save it.
Then we need to find graphics.
Main character virtual GUI.
Here we have wall jump sprite sheet and we're going to take all of them.
Place them on the animation and set sample rate to 15.
Nice.
Now let's go to assets and to the animator.
And over here we have this animation player wall slide.
We're going to transfer to this animation only if we jump in or fall in, because we don't want to enter
wall slide from any other animation, like if we're walking or standing, we do need to enter wall slide
animation only when we jump in on the wall.
Let's make a boolean.
Is wall detected.
And we're going to make transition to wall slide with a condition is well detected.
True.
Let's uncheck exit time and transition duration set to zero.
Now it's going to work like that, but not entirely correct.
But still I want to show you how it's going to work so you understand what we're doing here.
So as of now, if we walk to the wall nothing is going to happen.
But if we jump, we're going to enter.
Oh wait, I forgot to update the boolean.
I'm sorry.
Let's do this.
Let's take this name over here.
Go to player.
Let's find a handle animations.
And over here we need enum set bool.
He's well detected to is well detected.
Just like that.
Nice.
Now let's save it and go back.
And let's see this in the action.
I'm going to walk up to the wall.
Nothing is going to happen.
But if I jump, I'm entering wall slide and it's okay to have this animation when we going down, but
when we going up, we don't need to have a wall slide animation.
So over here we're going to add the same condition as we had in the method with the RB velocity dot
y.
Over here.
So we're going to add basically the same condition in the animator.
Let's go back hit plus on the conditions.
Find why.
Velocity less than zero.
We also need to exit this animation, and that would be done when we detect in the ground.
If we came to the ground, we are no longer wall sliding and we need to exit the animation.
So we're going to choose this condition over here and click is grounded.
True.
Let's uncheck exit time.
And now let's go and try again.
So we can see.
I'm going to the wall.
Nothing is happening.
I'm jumping and I'm entering one slide only when I'm going down and then I'm going back to the idle
animation.
Very cool.
Now, if I go off the wall with my movement key, just like that, we can see I'm still in a wall slide
animation, which is not really good.
So we need to fix it and we can fix it easily.
By setting alternative condition for the animation transition, we can just select it again make transition
to jump fall.
And this will work sort of like or condition.
We check both and you do this or this.
And for the second condition we're going to check.
If Isabelle detected false, and if it's a false, then we're going to exit this animation state.
Now let's go back and try this again.
Now we can get on the wall and we can get off the wall just by pressing movement to the opposite side
of the wall.
The only issue is that we can see that sort of a flip when we get off the wall, and it looks like a
glitch, and that happens because we getting off the wall and still we detecting a wall for a little
bit.
And we have that wall slide animation.
When we are off the wall.
It's very easy fix, although to be honest, I spent two days on finding whites like that and solution
was super simple, but it took me a long time to find it.
Yeah.
So before we apply the solution, let's fix another issue, which is also very important at the moment.
I can run into the wall just like that, and I don't really like it.
So I want to stop movement of the character if he's facing the wall and if he's facing the wall, we're
going to flip him only when we have X input.
Let's go back to the scripts and.
Over here in the.
Clip controller over here, where we handle flip instead of velocity of the rigid body, let's use x
input.
X input.
Now, flip function depends on the input of the player and not on the velocity, which is actually better
than it was.
Now let's go to handle movement over here and add simple if statement.
We're going to type if wall detected.
Then return.
If you see something like this for the first time, I'm just going to say that return basically means
interrupt the function.
If we return from a method, nothing after return will be applied.
It will not work.
Simple as that.
So we check if we detect in the wall, then we return from a movement means if we detect in the wall
then we cannot move.
It would be very similar to have something like this.
Yeah, but I just like it better with the return because in this way it's a bit easier to understand.
So this is what we're going to have.
So we changed the flip method and we changed handle movement.
Let's save this and let's go back to unity.
Now when we walking up to the wall.
We cannot move, but we can move to the opposite side because we flip and move.
Very good.
I like it very much.
Uh, something else there we can see that character is behind the wall while he is standing.
This is not as important right now.
I promise to you, we're going to fix it later.
Once we draw the level parts, I'm going to explain why he's behind the image and so on.
Although if you really want to fix it quickly, you can go to animator and choose Order and Layer one
over here and it's going to be fine.
But I'm going to talk more about sorting layers and ordering layers later on.
So this is just a quick fix for you, right?
One.
Now we cannot move when we try to move into the wall, but each with the flip is still there, right?
Look, I'm going to jump on the wall and I'm going to try to get off the wall.
And there is this sort of a glitch, a very fast one, but it's visible, it's noticeable, and it's
supposed to be smooth and nice and to fix it.
First, let me explain what is happening there.
So when we get an off the wall, the boolean that says is well detected or not.
Is not yet updated by the time we updating the animation.
Yeah, something like that.
Over here in the handle animation we pass is well detected to the animation boolean.
And the deal is that when we get off the wall, it's kind of like information is not updated fast enough.
Or maybe like information was updated long time ago.
And because of that we'll detect it still says we detect in the wall even though we are not detecting
the wall anymore.
And because of that, we can still see that sort of a glitch with the animation fix is very simple,
although again, I spent two days to find a solution for this.
Uh, we need to take this handle collision method in the update and move it closer to the handle animation.
So between collision and animations we have as less code as possible.
For that.
We're just going to take this method and move it down over here just like that.
So make sure you have input wall slide movement flip collision animation.
If something changes in the future I'll let you know.
But as of now, this is just good.
And let's save it and let's go back to unity and let's see it again.
And I'm sure you'll spot the difference right away.
Now I'm going to jump on the wall and I'm going to get off the wall and it smooth and nice without the
glitch over there.
You can see that right?
Very smooth and nice.
So now we have uh, sort of a super fresh information about wall detection when we updating the animator.
And that solves the problem.
Very nice.
I love it so much.
And we cannot run into the wall.
Also super cool.
There is an issue with a jump when we, uh, wall sliding, but that is just not yet fixed.
We're going to fix it when we're doing a wall jump, because when we press in space and doing wall slide,
we're going to go to wall jump.
And this will not happen at all.
All right.
There is something else I would like to do.
To improve the wall slide.
We're going to go to player script.
And in the handle wall slide we're going to do similar uh functionality like we did over here with the
wall detected.
But we're going to create a local variable.
Um.
If you never saw local variables before.
It's basically like any other variable in the script, but these variables are available to the entire
script, and local variable inside of a method is available only to the method.
So we can type here bool can wall slide equals to and we can.
Wall slide is going to be true only if is wall detected and rb velocity dot y is less than zero.
Okay.
Now let's go here and type.
If Campbell slide equals to false.
If we cannot slide we return.
So logic is very similar to what it was before.
It's just written sort of in an opposite way.
You know uh, like we make an a boolean.
That will be true only if all detected and velocity is below zero.
But if this boolean is false means we cannot slide anymore and we're supposed to return from this method,
and this code will not be applied.
All what we need to do now is to delete this if statement.
Now it is useless and it's going to work in a very same way, but it's just written differently and
maybe it does not really make sense to you.
Why are we doing it like that?
But I promise to you it is better because, um, we're going to add one more condition to the wall slide
later on, and it's easier to add condition over here.
And then just read console slide Boolean when you check in, what function does instead of reading an
if statement with triple condition and trying to understand what's the purpose of this if statement?
Basically, the main goal of this is to make it more readable, because now you can check if what if
can wall slide are okay and what is the condition for wall slide.
Oh this is a condition for wall slide okay I get it.
But when you have an if statement like we had before and you don't remember what you did in this method,
because I promise to you, after a couple of days you will forget what is written here.
So for now we're going to keep it just like that.
And I want to add one more improvement here.
I want to make another local variable float y modifier.
And we're going to use this modifier over here instead of hard coded number.
Uh we're going to type float y modifier equals to uh point.
Let's do 05F something really small.
Now my goal is to apply y modifier over here.
And I want to change this modifier.
If we press in key down because I want to make character slide faster, if we press and down so we can
have quicker movement around the map.
For that I need to know the y input we have.
So we're going to type here private float y input.
And then we're going to go back to handle input.
Go to definition.
Let's open this.
And over here we're going to do y input.
Equals to input.
Get axis.
Raw.
Vertical.
Nice.
Now we have y input, we can easily read it and we can keep it like that and do something like this.
If y input is less than zero means we press it down, then y modifier equals to one, right?
This is what we can do.
And let's just go there and make sure it's working.
And then I'm going to show you another way to write it.
That is a bit cleaner I would say.
So now I'm jumping on the wall.
I'm sliding slower than it was before.
And if I press key down I'm sliding fast.
So this allows me to do some sort of, you know, ju ju.
So I have more control of the character, which is very important in the platformer game.
Now this is working just like that, and I kind of like it.
There is another way to write it, and I want you to learn how to write it in a different way.
Maybe you will not remember how to use it, but next time I'm using it, you'll know how does it work
and what is it at all?
So we could do something like this if y input is less than zero.
Then.
Why modifier equals to one else why modifier equals to 0.05 f.
And this would be an if statement.
And there is a way to write the very similar if statement but in one line of code, and it will do the
same functionality, but it will take less of a space.
How does it work?
We go to float y modifier and before we assign the value, we can sort of ask a question and if answer
to the question will be yes, then it will take one value.
If answer will be no, then it will take another value.
This is how it works.
We type here y input less than zero question mark.
So this is the question over here.
Then if yes then use one.
If no then use 0.05 F.
And this is sort of a shortened if statement.
We can delete this now and it will work same as before.
Just want to explain once more.
So value of this modifier will be equals to either 1 or 0 five.
And it will depend on the condition over here if y input is less than zero if we press it down then
answer is yes.
And we're using first value over here.
That is after question mark.
It will be one.
If answer is no.
If this is wrong then we're going to use um.
While you after the columns over here.
So it's going to be this value.
Okay.
Yeah.
So let's just move it there and clean this up a bit.
Now we have a new method which is clean and nice and understandable.
And this works same as before.
And also it's not only clean method but also it's going to be good foundation for us to extend that
method in the future if we need to.
Okay.
Good.
I'll see you in the next video.