Nerd For Tech
Published in

Nerd For Tech

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.

https://docs.unity3d.com/2017.3/Documentation/ScriptReference/ControllerColliderHit.html

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.

Normal.

“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.
C#
  • Write the method to get the vector data from this class.
C#
  • 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.
C#

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store