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

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Azure Functions — Part 1

Dispatch Dev Update 7 —10/10/18

30 Ways to Write Better Python Code — Part 3

How to configure HTTPD and Py in Docker Container

High Order Methods In Ruby

Demonstrate The Value Of WIP Limits In 5 Minutes

Enabling Shift Left

Global Mosaics ver. 1.0

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
Samarth Dhroov

Samarth Dhroov

Work in Progress.

More from Medium

Cinemachine Refresher: Look At

Object Pool Pattern in Unity

Coroutines with Unity

What are Unity Skyboxes?