Wall Jumping in Unity
Objective: Implement the ability to wall jump!
Another common feature in platformers today is the ability to jump off of a wall. In this tutorial I’m going to share with you a method to implement that feature. We are going to begin inside the player’s script.
Since we are using a character controller we can implement the on controller collider hit method.
When do we want to wall jump? After we have jumped onto the wall so we need to check if our player is grounded.
Going back into unity, select the walls we are wanting to jump off of and tag them as “Wall”
Add this tag to the parameter to check if the player is colliding with when jumping.
When these parameters are met we want to draw a ray perpendicular to what we touch.
The logic here is to be able to jump off this wall would be that were going to get the impact point and then the direction to bounce off is the surface normal, which is to the right, or if the impact is to the left then the surface normal would be to the left.
Another change we need to make to save ourselves from future headaches we need to make it to where we cannot change our direction mid air. we want to only be able to modify our direction/ velocity if we are grounded. Make direction and velocity global variables and place the function inside the is grounded check.
With this taken care of, we need to add another bool to check if we can wall jump.
In the is grounded we need to place the bool in there as well this way the bool can be turned off.
Now we need to add a parameter to the double jump, if can wall jump is false, double jump. If wall jump is true, we want to wall jump. The wall jump’s direction is already determined, however, we need to create a new Vector3 variable.
On the controller collider hit method we need to get the direction and assign that direction to the global variable so we can use it in the if method above.
Now, when the can wall jump bool is true, and the player presses the space key we need to add the jump height to the y velocity. Then we need to make the velocity equal the new vector3 and multiply by the speed. this will give the player the “push back” from the wall.
Now when the conditions are met, you player can now preform the wall jump!
You can now remove the draw ray and I’ll see you in the next tutorial!