How to climb stairs as a Rigidbody (in Unity3D)


Using: Unity 3D5

So you want to add some stairs to your game? Great! Adding stairs in level design adds more vertical variety to your levels. Or perhaps your game is based on stairs (Stair Climbing Simulator 2018?).

In this tutorial I'll show you how to implement stair climbing from scratch in Unity3D if you're using a Rigidbody as your player. Before we get to coding, you should consider your (much easier / less work) alternatives:

  1. Use a CharacterController. It already has some support for this, but can't have physics applied willy nilly, nor is it as customizable as a Rigidbody with multiple colliders.
  2. Use a CapsuleCollider with your Rigidbody. If you get the player going fast enough they can just ram right over the stairs.
  3. Use ramps or stairs with invisible ramps over the steps. They work just as well and even provide a smooth transition surface. Doesn't work for cases of dynamic/physics content or player generated content.

If you've looked into these and you still need stairs, let's dive in.

Scope and Requirements

This tutorial uses Unity3D and C# but the concepts should be the same (except for the Unity specific quirks I mention). The stair stepping style is based off of the behavior found in the Source Engine.

You should also have a working MonoBehaviour script for simple player controls that uses a Rigidbody component.
It should be able to move at a minimum. If you don't, RigidbodyFPSWalker is a decent start

Configurable Options

The stair stepping can be tweaked in numerous ways. For now, we will define only two parameters explicitly that will be used later in the tutorial.

public float maxStepHeight = 0.4f;		  // The maximum a player can set upwards in units when they hit a wall that's potentially a step
public float stepSearchOvershoot = 0.01f; // How much to overshoot into the direction a potential step in units when testing. High values prevent player from walking up tiny steps but may cause problems.

A quick look at ContactPoints

The physics engine in Unity3D, PhysX, generates collision events
(OnCollisionEnter, OnCollisionStay, ...). These collision events return a Collision object with gobs of information.

A nifty little property on this object is the Collision.contacts member which gives a list of all of the points of contact in a collision (well, usually all of them, sometimes it doesn't but only with complex collision MeshColliders hitting each other).

The ContactPoints come with a position, a normal, and collider information.

For visualization, here's a collision between my player (a green BoxCollider), a surface, and the resulting ContactPoints. The ContactPoints are visualized as blue lines.


Getting the ContactPoints

We need all the ContactPoints every physics frame. To capture them we save all ContactPoints generated by both OnCollisionEnter and OnCollisionStay events.

List<ContactPoint> allCPs = new List<ContactPoint>();

void OnCollisionEnter(Collision col)
void OnCollisionStay(Collision col)

We need to loop through all the ContactPoints and then do the stair step.
It is best to do this in FixedUpdate as we're working with physics data which is only generated every physics frame.

void FixedUpdate()
	//Do stair stepping
	allCPs.Clear(); //Deletes all ContactPoints collected from the last physics frame

Detecting ground and a stair

Now that we have all the ContactPoints for the player we need to look through them for some specifics

For the rest of the tutorial this will be the example situation we'll be looking at


Detecting the ground

Detecting the ground must be done before looking for stair steps. We can say that ground is any ContactPoint with a Y (up) normal component of greater than 0.0001 meaning that it has some sort of
upward direction to it.

/// Finds the MOST grounded (flattest y component) ContactPoint
/// \param allCPs List to search
/// \param groundCP The contact point with the ground
/// \return If grounded
bool FindGround(out ContactPoint groundCP, List<ContactPoint> allCPs)
	groundCP = default(ContactPoint);
	bool found = false;
	foreach(ContactPoint cp in allCPs)
		//Pointing with some up direction
		if(cp.normal.y > 0.0001f && (found == false || cp.normal.y > groundCP.normal.y))
			groundCP = cp;
			found = true;
	return found;

From this function we are able to determine if the player is grounded as well as at what Y value the player is grounded.

//In FixedUpdate
ContactPoint groundCP = default(ContactPoint);
bool grounded = FindGround(out groundCP, allCPs);

Detecting a stair

Detecting a stair is a much more intensive process that requires a lot of math. FindStep does very similar things to FindGround above.

First, we don't care about any stairs if the player is not moving.

Second, the actual checks are done in ResolveStepUp but the process is the same, check every ContactPoint.

/// Find the first step up point if we hit a step
/// \param allCPs List to search
/// \param stepUpOffset A Vector3 of the offset of the player to step up the step
/// \return If we found a step
bool FindStep(out Vector3 stepUpOffset, List<ContactPoint> allCPs, ContactPoint groundCP, Vector3 currVelocity, Vector3 playerIntent)
	stepUpOffset = default(Vector3);
	//No chance to step if the player is not moving
	Vector2 velocityXZ = new Vector2(currVelocity.x, currVelocity.z);
	if(velocityXZ.sqrMagnitude < 0.0001f)
		return false;
	foreach(ContactPoint cp in allCPs)
		bool test = ResolveStepUp(out stepUpOffset, cp, groundCP, currVelocity, playerIntent);
			return test;
	return false;

Now we start building ResolveStepUp

/// Takes a contact point that looks as though it's the side face of a step and sees if we can climb it
/// \param stepTestCP ContactPoint to check.
/// \param groundCP ContactPoint on the ground.
/// \param currVelocity Current velocity of the player
/// \param playerIntent The players current intent vector (based on the keys they're pressing)
/// \param stepUpOffset The offset from the stepTestCP.point to the stepUpPoint (to add to the player's position so they're now on the step)
/// \return If the passed ContactPoint was a step
bool ResolveStepUp(out Vector3 stepUpOffset, ContactPoint stepTestCP, ContactPoint groundCP, Vector3 currVelocity, Vector3 playerIntent)
	//Determine if stepTestCP is a stair...
	return true; //If stepTestCP is a stair

For ContactPoints to be considered a stair, they have to remotely look like a stair. When the player hits the stairs, it's like hitting a really short wall and it will generate
ContactPoints with really low Y (up) directional components for their normals.
So, for each ContactPoint, we only used the ones with low Y normal values

//( 1 ) Check if the contact point normal matches that of a stair (y close to 0)
if(Mathf.Abs(stepTestCP.normal.y) >= 0.01f)
	return false;

Considering this check says nothing about the height at which this ContactPoint occured, it should filter out all the ones that are too high for the player to step up to

//( 2 ) Make sure the contact point is low enough to be a stair
if( !(stepTestCP.point.y - groundCP.point.y < maxStepHeight) )
	return false;

We've now gathered quite a bit of data.


Determining where to step up

We've detected a stair, this is cool, but we don't know how to get up it.

//( 3 ) Check to see if there's actually a place to step in front of us
//Fires one Raycast
RaycastHit hitInfo;
float stepHeight = groundCP.point.y + maxStepHeight + 0.0001f;
Vector3 stepTestInvDir = new Vector3(-stepTestCP.normal.x, 0, -stepTestCP.normal.z).normalized;
Vector3 origin = new Vector3(stepTestCP.point.x, stepHeight, stepTestCP.point.z) + (stepTestInvDir * stepSearchOvershoot);
Vector3 direction = Vector3.down;
if( !(stepCol.Raycast(new Ray(origin, direction), out hitInfo, maxStepHeight)) )
	return false;


//We have enough info to calculate the points
Vector3 stepUpPoint = new Vector3(stepTestCP.point.x, hitInfo.point.y+0.0001f, stepTestCP.point.z) + (stepTestInvDir * stepSearchOvershoot);
Vector3 stepUpPointOffset = stepUpPoint - new Vector3(stepTestCP.point.x, groundCP.point.y, stepTestCP.point.z);
return true; //We're going to step up!

How to step up

We hit something, figured out it was a stair, and even got the position to where we're going to step up. We did this all in the beginning of FixedUpdate and we will now check at the end whether to step up or not.

	//Take the RigidBody and apply the stepUpOffset to its position
	this.GetComponent<Rigidbody>().position += stepUpOffset;
	//When it hit the stair, it stopped our player, so reapply their last velocity
	this.GetComponent<Rigidbody>().velocity = lastVelocity;

Other checks

Keep in mind the only real restriction that we placed on the player was that they can only step up things with a horizontal normal and things that are under a specific height. A player might step up into a space where they don't have enough head room or perhaps the step is not big enough to classify as a "step". It's up to you to add these extra checks and to balance performance with glitch reduction/accuracy.

Verizon Did Some Cool Shit

I saw this video by SethBling (above) today and while it’s not vastly exciting or landmark it demonstrates…