Wall Jumping in Unity
Objective: Add logic for the Player to wall jump
The first thing we need to do is move our Vector3 variables direction and velocity from local inside the update to global variables. Then we need to make sure we can’t change direction once we have jumped so we move the direction and velocity logic inside the if is grounded so once we have jumped we can’t change the direction.
Next we need to tag the wall platforms so the scripts can identify them from the ground platforms. This way we can tell when the Player hits the wall instead of the ground so we can enable the wall jump.
We need to use the OnControllerColliderHit method using a ControllerColliderHit called hit. Inside this we run a check if the controller in grounded and hit transfrom tag is wall. We need to set a global bool for if the Player can wall jump and we need the surface normal from the wall so we create a Vector3 for wall surface normal.
Inside the OnControllerColliderHit we set the wall surface normal to the hit normal and set can wall jump to true.
We move back to the update and if the controller is grounded we set the can wall jump to false since we only need this enabled when we are in the air and hitting a wall. We then move to the else logic meaning when the Player is in the air. Here we change the logic so the if space key is pressed and can wall jump is false we check if double jump is true then before setting the jump velocity and setting the can double jump to false.
We then have an alternative if space key and wall jump is true before continuing to set the jump velocity and the Players velocity to the wall surface normal * speed. We then continue with the already implemented movement logic.
We have now split the logic of double and wall jumping. In the next post we will look at pushing objects in Unity.