Jumping in between two walls in Unity
The exercise here demonstrates implementing wall jumping in Unity.
Without going further into anything, an in built class from the Unity Engine Library has to be understood.
Unity provides an out of the box class that addresses collision based matters.
The player object has a character controller component attached which would allow the usage of the class above.
In the class here, there is a property which solves the task here.
“Surface normal is a vector perpendicular to the surface at a specific point.”
As seen in the image above, the player’s intention while jumping in between is to get a bounce back from the colliding wall and use that momentum to head upwards until reached to the target position.
This surface normal is available in the class mentioned above.
Below is the simple implementation in the player script component.
- Create two private variables. One would be a flag and Second would be storing the normal vector.
- Write the method to get the vector data from this class.
- In the Update method, check for this condition. ( Player is not on ground and _canWallJump is true which means the player has just collided with the wall.)
- If satisfied, add the normal vector to its velocity multiplied by a certain speed variable. Also, add jump boost to the Y direction speed of the player.
Please keep in mind that the code above shall be inserted keeping other checks as per your program.
That should be it :)
Here is the final result.
Thank you very much