How would one approach wall-running for rigidbody based controllers?

Hi guys,

I want to have my FPS character do wall running and been wondering how this could be achieved?

Thanks!

Probably with some smart use of isKinematic and script control of how should it be.

A pretty open-ended question… Have you prototyped anything, or maybe tried any code snippets you might have found?

I’ve never tried this before, but here’s where I think I would start:

-In OnCollisionEnter(), If contacting a wall object AND the mean normal of the contacts is horizontal (low y-component) AND the angle between transform.forward and the contact normal is close to 90 or some other determined value (wall is to the side of the character) AND the Rigidbody is going fast enough, then we enter the wall running state. Disable gravity for this Rigidbody. Use a variable to store whether this is a left- or right-hand wall based on the contact normal. Would consider giving the character a speed boost here. Handle animation here, too, in some way.

-In Update(), check if we’re in the wall running state. If we are, cast a ray from the center of the character in the direction of the wall-- the length of the ray depends on whether you want the character to be able to run around outside curves, with longer rays for sharper angles where the character will still stick to the wall, and the shortest ray possible being so that it just barely contacts a straight wall.

-Check whether the above raycast hits the wall. If it does, set the character’s transform.right to the inverse of the RaycastHit’s normal to align the Rigidbody with the surface of the wall. Probably should also nudge the character’s position to the RaycastHit point plus an offset (the fixed distance from the character’s center to the point that is supposed to stick to the wall).

-Check whether the Rigidbody slows down too much. If so, disengage from the wall by exiting the wall running state, enabling gravity, and handling animation.

-Check whether the object has been in the wall running state for too long. If so, disengage from the wall.

-Will need to disable some of the controls when the object is in the wall running state to prevent the player from rotating or speeding up. Other controls will need to have alternate behavior. The ‘backward’ button or axis could probably slow the speed of the wall run. The jump button will now have to run the wall run disengage logic, as well as jump at an angle away from the wall.

I think some other velocity manipulation of some kind will need to take place in order to make the object stick to the wall correctly. I’m also not sure how advisable modifying transform.up, .forward, and .right are for Rigidbody objects. No doubt this is not flawless, but I think it could lead to a solid wall running method. I might try to make something that follows this general algorithm to see how well it works…