 So now our character is walking on slopes perfectly and this is what we needed for platform games. Stop! Before this video starts, I'd like to say that I am actually stunned. Last week I saw that we reached 5000 subscribers and this just completely blew my mind. I really don't know how to deal with that. I have no idea this is why sometimes I don't celebrate these milestones because I am just completely... I don't know what to do. So I would like to know from you what can we do to celebrate this because this is such a huge milestone. So I would like to thank you a lot for following me and following my project and just being so supportive with me. So with that said, please comment below, give me some idea on how we can celebrate that. But that said, let's start with the video. When making platform games, everything is good and fun until you have to work with slopes. Right? Here is when things start to get messy. So this is the problem that we'll try to tackle in this video, how to make and code slope movement in Godot Engine. So I have prepared this level using the Godot Sandbox asset that I have, the asset pack. And if you don't know about the Godot Sandbox pack, you can check it out in the link in the description. This is an asset pack sponsored by my patrons. So if you want to support this project, you can also check out the description to support me. I have also set this screen key display so at any point you can see what I'm typing. I also have prepared a script for the character movement. This is a very simple script. We have a method to update the direction that is triggered by the player input. So if the player press either left or right, it will update the movement direction. But this is not the focus of this video. If you want to pause now and copy this method, you can pause because I will fold this in 3, 2, 1, fold it. I will also fold this other method here, this callback. And let's focus on the physics process here. So if we try to test this script, you can see that the character has a very weird movement and seems like it is sliding on ice, right? So this is because we are setting the character velocity to be the reminder of this movement. And the reminder of this movement takes into account the floor normal. So if the character is moving in this direction, the floor normal will be something like this and the character will be pushed back in this direction and downwards. So we don't want that. When I am scripting the movement of characters for platform games, I often just mess with the vertical axis. So with the y factor of this vector. So if we try to make this velocity, we will only change the vertical movement of the character based on the reminder of this movement, which is exactly what we want. Because when we are moving on a slope, so if I go back to this, I'm using Godot 3.2, the release candidate that was released recently. So we have this measure 2 now, which I love by the way. So I will zoom in. So when we are moving a character with the move and slide, the character will be moving towards this direction and it will collide with something with this angle, right? With a 45 degrees angle. This movement that is going only on the horizontal will have to be slide so that the character can continue the horizontal movement, but without any collision. So what the method will do is that it will push the character upwards until it stops colliding with this slope. And this upwards offset is what we want in order to move our character in a slope. So that's why I'm only changing the vertical velocity here. Because I want to keep the momentum of the character horizontally. With that, you can see that the character can stop on a slope. It will slide, right? It is sliding down because we are adding a force downwards, the gravity, and it's trying to push it down in order to make this collision stop. So the way it does that is that it will push it on this direction until it can't offset it anymore. The way we can make it stop sliding on a slope, let's go back here, is by adding another argument in this method. So if we try to check out the documentation for this method, move and slide, we can see that we have the linear velocity that we want the character to move. We have the direction of the floor, which is basically the up direction, a normal pointing up. And we also have a Boolean that is the argument if this character should stop on a slope. And if we go back, this is exactly what we want. We want it to stop on slopes. So let's trigger this through. And now you see that the character stops sliding down the slope. So this is exactly what we want. But now we have another problem, right? The character is kind of like bouncing when we stop on a slope. And if we come from a slope and then we go to a flat floor, the character will also bounce. And let's see why. So I will go back to the 2D mode. So basically the character movement is a, I will clear this and hide this tab here. So when the character is moving in this direction, and since we are updating the vertical velocity, the vector that is created from this force is a vector in a 45 degrees because this slope is 45 degrees, right? So we keep moving. So we start here. We keep moving. We keep moving. And when we leave this slope, the momentum of this movement is something like this. So we have this reminder force when we leave this slope. And this is because we are not snapping to the ground. Because if we were snapping, we will do this movement. And as soon as we leave this slope, the character will snap to the ground and will continue its horizontal movement. And this is what we want, right? So going back here, how we can obtain that? How we can obtain this snapping? Well, turns out that Gerrard has a very simple way to achieve that. We have the move and slide with snap method. But this method has a different structure for its arguments. So let's start from the beginning. We need to pass the velocity. We also need to pass the snap vector. And notice that this is a snap vector. It's not just the directions, not just the normal. Different from the move and slide method, which asks just for the floor normal. This one needs a snap vector. And this is because it will take both the direction and the length of this vector to know if the character should be snapped or not to the ground. So if you fall from a cliff, it won't immediately snap the character to the floor. In order to create this snap vector, we can use two constants. That will be the snap normal, which will point down. We can actually name this snap direction. I think that this will be better direction. And this will point down. And we'll also use a snap length that I think that 32 pixels will do the trick for us. Because what we want to do is that it will take the kinematic body and it will cast a vector just like a ray cast pointing in the snap direction that we gave with the snap length. So it will be basically something like this. And I am using 32 pixels. And I think that this will be enough. So going back to the script, we will create a variable that will be the snap vector. And this will take the snap direction multiplied by the snap length. And you might be asking yourself why we are using a variable, right? So we are using a variable because sometimes, especially if you want our character to jump, we'll need to change this snap vector to zero, so a vector with a zero length. This is so that the character will not snap to the ground anymore. And then we can apply a vertical force pointing upwards. And the character will not be snapped to the ground anymore because this snap vector will have both a direction of zero and a length of zero. But we want to have these other constants here so we can come back to this snapping when the character hits the floor again and we want it to be snapped to the floor again. So now that we have this snap vector, we can use it as the second argument for our function. And then we can pass the floor normal. I think that I will skip this line. And just so we can keep everything organized, I will double tab here and use the floor normal. We need to stop on slopes. And let's try this. So if we test that it said, oh, OK, so I, yeah, I deleted the we are trying to assign a vector to a variable that is a float. So now we are trying to access only the vertical axis of this vector. Now if we test this, I will move the character from the slope to the flat floor. And you can see that we don't have this momentum anymore. The character will be snapped to the flat floor, but we now have a problem, right? First of all, the character is not moving as we wish. So, for instance, if I come from the flat ground to the slope, the character will basically float and we don't want that. And another thing is that if we try to stop on the slope, the character will bounce. We didn't have this previously and now we have. So we need to fix that. So I will stop this testing. And the reason why the character is bouncing when we stop on slope is because I will go back to the to the workspace and I will measure the angle of this slope. So this loop starts here and it finishes here. So we can see that this is a 45 degrees slope. What that mean? Why am I imagining that? So if we go to this move and slide with snap, we can see that the floor maximum angle is zero dot this number here. And well, I don't I don't know how to work with radians. But I know that this floor here, this value here will give us a value of 45. So this is the angle that a floor will still be considered to be a floor and not a slope. So let let me repeat this so that you understand. Until this angle until this 45 degree angle, things will be the ground will be considered to be a floor. After that, it will be considered to be a slope. So we need to decrease this value in order to decrease the threshold of a slope. So going back to the player, let's create a constant that will be floor max angle. And since I don't know how to work with radians, I'll use them back to radians methods so we can pass on a degree and it will convert this to a radian. And let's say we want the floor, the collision to stop be considered a floor after 15 degrees. So I think that this threshold is very low so we can still use some small slopes. And let's copy this. And I think that we still have this max slides and I will keep this default value as this argument. And now we reach the max floor angle and let's paste this value here. So let's test this and the character is still bouncing. So let me check this again. So we are using the floor maximum angle. And I think that this will be the limit for the floor. So above that, things will be considered to be a slope. I think that I think this is not what this does. If I change this to 46 degrees, I think that this will work. Yeah, you can see that this is working. So just for the sake of naming, I will not use floor max angle because this doesn't quite describe what this argument does, what this what this value means. I will actually use slope threshold. This is more meaningful in this context. Save, slope threshold, yeah. So you can see that and we fix both problems with just this single change, this single extra argument. So now our character is walking on slopes perfectly. And this is what we needed for our character movement for platform games. So that's it. That's the basis of how you can handle slope movement for platform games in Godot Engine. I would like to thank you so much for watching. And also don't forget to leave suggestions in the comments for how we can celebrate the 5,000 subscribers milestone, please. I would also like to thank my patrons for sponsoring this video. Thank you so much for support. So that's it. Thank you so much for watching. Keep developing and until the next time.