Platformer: Wall Jumping Setup
Intro
Wall jumping is a corner stone in any platformer. It allows the player to climb up any set of walls by by having them continually jump off the wall in the opposite direction. There a few things we need to do in order to implement a successful wall jump code.
There are two things we need to know…
- When the player has hit the wall.
- Calculating the surface normal, which is the reflection of the contact point or direction that will be perpendicular of the contact point.
Using On Controller Collider Hit, which detects when the player control comes into with another collider, we use debug draw ray to show the surface normal from the hit point. This will show the reflection we will need in order to jump in the opposite direction of the wall we are coming contact with.
The Code
We will need to change how we calculate the player’s direction and velocity. Previously, we were calculating the direction and velocity when the controller wasn’t grounded. This allow the player to move while in the air. This will cause issues with the wall jumping mechanic. We will change the direction and velocity calculation when the controller is grounded. This requires the direction and variable to me changed from local to global.
Once this is done, we move the calculations inside of the controller grounded check.
This now commits the player to their jump direction and no longer allows them to move once inn the air.