Finally, A Good Wall Run In Unity

Rohan Naphade
3 min readMar 3, 2021

Wall running is hard to get right. It can add to the movement of a game and can also take away. It is important to remember that just the mechanics of the wall run being perfect will not make it feel good. Visual effects are required to make the mechanic more believable. This article will walk you through making your very own wall run.

Prerequisites:

You will need a first-person rigidbody based character controller which rotates a rigidbody on the Y-axis and the camera on the X-axis. If you do not have specifically this, you can read the article, understand the code, and modify it to fit your use case. If you wish to use the character controller I used to test this mechanic, scroll to the bottom of the article and click on my github link. Make sure that your rigidbody’s mass is 1 and its drag is anywhere from 2–4 depending on how snappy you want the movement.

Step 1: Detecting the wall

I have seen many approaches. Some of them lead to sacrificing the types of wall runs possible while others lead to limited options in level geometry. The two main approaches I see are raycasts which limit wall running capabilites, and checking if a collision is of type wall which makes you label every single wall that you can run and makes it impossible to use certain geometry like a wall you run on and also stand atop. A good solution that I came across uses the OnCollisionEnter() function along with some basic vector calculations to check if the player is not atop or beneath the platform.

As you can see I check if we can wall run and in that case, add an upward velocity to our rigidbody based controller as well as a starting forward force. You should tweak these values to fit your use case. I also set an isWallRunning boolean to true so that we can continue applying forces in the update function. Now we need to find a way to know when we stop the wall run.

Now when we either exit a collision or fall to the ground, our wall run is stopped and a cooldown is initiated so that we cannot perform multiple wall runs in succession allowing the player to skip the geometry of the level. Now, for the actual wall run logic;

We add forward and downward force to the player making sure to multiply by Time.deltaTime so that framerate does not affect the speed at which we move the player. This works because every frame this value is multiplied by the delta time(the time between frames). We also check for possible wall run scenarios in our jump function using raycasts and jump based on these. A better approach to this and what I recommend you do is create a boolean array of states and setting each state in the update function and then making the if statement for each state. I, however, am lazy and did not do so even though it would be very little extra work. Again fine-tune these numbers to your liking. I have these numbers hardcoded for the purpose of the tutorial however you should convert these to private serialized fields which you can change in the inspector. Now for some camera effects;

What this does is tilts your camera every frame while you are wall running until your camera’s angle is what we want it to be. Snapping the camera to this angle would completely ruin the illusion. Linear interpolation may be a better approach to this or using a tweeting tool such as Dotween. However, I kept it simple with this script. I also increase the field of view to give the wall run a sense of acceleration. This is it! You should have an immersive and dynamic wall run mechanic to give your player an exhilarating movement option. Here is a Github link to the movement script. Feel free to use the script and credit RohanOpenSource. Comments and feedback are welcome. Thank you.

--

--

Rohan Naphade

13 years old. Code in java, c#, and python. Currently doing ML/DL in Tensorflow 2