How to climb stairs as a Rigidbody (in Unity3D)

How to climb stairs as a Rigidbody

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](http://docs.unity3d.com/ScriptReference/Collision-contacts.html) 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.

A contact point

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)
{
    allCPs.AddRange(col.contacts);
}
void OnCollisionStay(Collision col)
{
    allCPs.AddRange(col.contacts);
}

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

Player and it's collider approaching a step

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)
{
    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);
        if(test)
            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 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)
{
    stepUpOffset = default(Vector3);
    Collider stepCol = stepTestCP.otherCollider; //You'll need the collider of the potential step for this
    //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 generateContactPoints 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.

A player and collider contacting the stair at a given point with certain pieces 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;
}

Raycasts for determining whether we can step up or not

//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.

NOTE: You'll need the last physics frame velocity in order to make the step feel smooth as the player is stopped by the time we read the ContactPoint where they hit the wall.

//In FixedUpdate()
Vector3 stepUpOffset = default(Vector3);
bool stepUp = false;
if(grounded)
    stepUp = FindStep(out stepUpOffset, allCPs, groundCP, velocity);

if(stepUp)
{
    //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; //You'll need to store this from the last physics frame...
}

Full code

Combining everything above, here's the full code put together. You will have to modify this to play nicely with your version ofRigidBodyFPSWalker or the like.

[RequireComponent (typeof (Rigidbody))]
//Also requires some sort of collider, made with an AABB in mind

/// The class that takes care of all the player related physics
/// Many configurable parameters with defaults set as the recommended values in BBR
public class StepClimber : MonoBehaviour {
    //Remember 1 unit is 1 meter (the physics engine was made with this ratio in mind)
    
    [Header("Steps")]
    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 small steps but may cause problems.
    
    private List<ContactPoint> allCPs = new List<ContactPoint>();
    private Vector3 lastVelocity;

    void FixedUpdate()
    {
        Vector3 velocity = this.GetComponent<Rigidbody>().velocity;
        
        //Filter through the ContactPoints to see if we're grounded and to see if we can step up
        ContactPoint groundCP = default(ContactPoint);
        bool grounded = FindGround(out groundCP, allCPs);
        
        Vector3 stepUpOffset = default(Vector3);
        bool stepUp = false;
        if(grounded)
            stepUp = FindStep(out stepUpOffset, allCPs, groundCP, velocity);
        
        //Steps
        if(stepUp)
        {
            this.GetComponent<Rigidbody>().position += stepUpOffset;
            this.GetComponent<Rigidbody>().velocity = lastVelocity;
        }
        
        allCPs.Clear();
        lastVelocity = velocity;
    }
 
    void OnCollisionEnter(Collision col)
    {
        allCPs.AddRange(col.contacts);
    }
 
    void OnCollisionStay(Collision col)
    {
        allCPs.AddRange(col.contacts);
    }
    
    /// 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;
    }
    
    /// 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)
    {
        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);
            if(test)
                return test;
        }
        return false;
    }
    
    /// 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 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)
    {
        stepUpOffset = default(Vector3);
        Collider stepCol = stepTestCP.otherCollider;
        
        //( 1 ) Check if the contact point normal matches that of a step (y close to 0)
        if(Mathf.Abs(stepTestCP.normal.y) >= 0.01f)
        {
            return false;
        }
        
        //( 2 ) Make sure the contact point is low enough to be a step
        if( !(stepTestCP.point.y - groundCP.point.y < maxStepHeight) )
        {
            return false;
        }
        
        //( 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);
        
        //We passed all the checks! Calculate and return the point!
        stepUpOffset = stepUpPointOffset;
        return true;
    }
}

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. There are a few corner cases with this method including

  • Making sure that if the player creeps back down a step, it doesn't try to step them back up.
  • Player is too wide to step up into the given area.
  • When a collider is deleted in between physics frames (null checks needed on colliders in ContactPoints)

but these I'll leave these up to you to implement. They build off of the checks in ResolveStepUp