Platformer: Wall Jump
In this article, we will talk about adding a Wall Jump to a platformer. This works when the player jumps and hits a certain wall, it will reverse the direction and
We are going to be using OnControllerColliderHit, which can detect a Collider of on an object and differentiate it using a tag “Wall”.
We are using the OnControllerColliderHit method that will capture the Collider is coming into contact with in the variable hit. It is ONLY capture the other collider if the Controller is performing a move.
We check the tag to see if we are colliding with a wall jumpable wall. The global variable _wallSurfaceNormal is going to the store the hit.normal. The hit.normal return the opposite value of the hit point. For example, if we hit the wall on the right side of the controller it will return an x value of -1. We will also set a global bool _canWallJump true.
You can see how the hit.normal will return either a negative or positive 1 on the axis it is colliding with. If you hit the right side, -1 is returned and if you hit the left side, 1 is returned.
If the controller is grounded we will want to set _canWallJump false.
When the controller is not grounded (we are in the air), we hit the wall which sets _canWallJump true, and hit the space bar we are going have the player wall jump.
We set the direction of the Character Controller to the _wallSurfaceNormal, which is returning the opposite value of where it is hitting and multiplying by the speed of the character. This will give the player a bounce off the wall.
Once the direction is calculated we want to flip the player is they are facing the way they are moving. If the the direction on the X-axis is less than 0 (facing left) we rotate the player on the Y to -90 to face left and visa versa. we must make sure to set the localEulerAngles to these new values.
This facing variable is a Vector3 and set to the eulerAngles, which is the rotation of the X, Y, Z.
Finally, we trigger the Wall Jump animation, we add the _jumpHeigh to the direction on the Y-axis and set _canWallJump back to false, so we don’t get the Flappy Bird effect.
All of this is done before the Character Controller move is calculated.
This is a fun mechanic to add to your platformer and allows the level to really open up for the designer when the player has this ability.