Slopes, Steps and Ground Snapping
Collide and Slide keeps a character out of walls. Floors need three more rules, and they are what makes a controller feel solid or slippery:
- Walkable slopes: which surfaces count as ground, and what happens on the ones that don't.
- Steps: climbing a stair or a curb without jumping.
- Ground snapping: staying glued to the ground when walking over a crest or down stairs, instead of launching into the air.
Walkable or not: compare the normal with up
The angle of a surface is the angle between its normal and the up vector, and that is a dot product away (see Dot Product). Precompute cos(maxSlope) once, then a surface is walkable when dot(normal, up) >= cos(maxSlope). No acos per contact.
On a surface that is too steep, the character should slide down. The slide direction is gravity projected onto the surface plane, g - n * dot(g, n): the same projection as collide-and-slide.
#include <iostream>
#include <iomanip>
#include <cmath>
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
int main() {
std::cout << std::fixed << std::setprecision(3);
const double PI = 3.141592653589793, maxSlopeDeg = 45.0, g = 9.81;
const double minUpDot = std::cos(maxSlopeDeg * PI / 180); // compare dot products, not angles
std::cout << "walkable if dot(n, up) >= " << minUpDot << "\n";
std::cout << "slope normal dot(n,up) walkable slide accel\n";
for (int deg : {0, 20, 44, 46, 60, 80}) {
double a = deg * PI / 180;
double nx = -std::sin(a), ny = std::cos(a); // surface normal of a slope rising to the right
double upDot = ny; // dot(n, (0, 1))
bool walkable = upDot >= minUpDot;
// gravity projected onto the surface plane: g - n * dot(g, n)
double gx = 0, gy = -g, gn = gx * nx + gy * ny;
double sx = gx - nx * gn, sy = gy - ny * gn;
double slide = walkable ? 0.0 : std::sqrt(sx * sx + sy * sy);
std::cout << std::setw(4) << deg << " (" << std::setw(6) << z0(nx) << ", " << ny << ") "
<< upDot << " " << (walkable ? "yes" : "no ") << " " << slide << "\n";
}
}walkable if dot(n, up) >= 0.707
slope normal dot(n,up) walkable slide accel
0 ( 0.000, 1.000) 1.000 yes 0.000
20 (-0.342, 0.940) 0.940 yes 0.000
44 (-0.695, 0.719) 0.719 yes 0.000
46 (-0.719, 0.695) 0.695 no 7.057
60 (-0.866, 0.500) 0.500 no 8.496
80 (-0.985, 0.174) 0.174 no 9.661
The 44° slope is walkable and the 46° one is not, and the slide acceleration grows toward the full 9.81 m/s² as the slope approaches a wall. Engines expose this threshold directly:
| Engine | Setting | Default |
|---|---|---|
Unity CharacterController |
slopeLimit (degrees) |
45 |
Godot CharacterBody3D |
floor_max_angle (radians) |
0.785 (45°) |
Unreal UCharacterMovementComponent |
Walkable Floor Angle, SetWalkableFloorAngle() |
44.765° |
| Rapier character controller | setMaxSlopeClimbAngle() (radians) |
check the docs of your version |
Tip: Compare against the floor normal from the collision, not the slope of the visual mesh. A capsule standing on the edge of a step touches the step's rounded corner, and that contact normal can be tilted by any amount. Many controllers cast a short ray straight down from the centre to get a second opinion on the real floor.
Steps: up, forward, down
A step is a short vertical wall. To the slide logic it looks exactly like any other wall, so the character just stops. The usual step-up trick is a three-part move:
- Up: sweep the shape up by the max step height (or less, if there is a ceiling).
- Forward: do the horizontal move from up there.
- Down: sweep back down by the same height. If it lands on a walkable surface, accept the stepped position; otherwise throw it away and use the normal, blocked result.
Unity's stepOffset and Unreal's MaxStepHeight are that height. Godot's CharacterBody3D has no built-in step-up: Godot projects either accept that stairs need ramps (invisible collision ramps over stairs are common in every engine) or implement the up-forward-down sweep themselves with test_move() or shape casts.
Ground snapping
Walking up a ramp gives the character an upward velocity. At the top, the ground falls away, but the velocity is still pointing up, and the character takes off like a ski jumper. The same happens walking down stairs: each stair edge is a small cliff, and the character "bounces" down the staircase in tiny falls, losing the grounded state (and the ability to jump) on every stair.
Ground snapping fixes both: if the character was grounded last step and the ground is within a small snap distance below it now, move it down onto the ground and keep it grounded. Godot calls the distance floor_snap_length (default 0.1 m) and also offers apply_floor_snap(); Rapier has enableSnapToGround(distance); Unity's CharacterController has no snap setting, so Unity projects usually add a small downward move each step or a ground probe.
Snapping is turned off for the step when the character jumps, otherwise it snaps straight back to the floor.
A walk across a tiny level
The model below reduces everything to a heightfield: the level is a list of pieces, each with a straight top. The character walks right at 2.5 m/s with a 0.1 s step, can step up 0.3 m, and walks over a 0.2 m step, a 20° ramp, a crest, a 29° slope, a 0.15 m stair down and finally a 0.6 m ledge. It walks the course twice, without and with ground snapping.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <string>
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
// The level is a list of ground pieces [x0, x1) with a straight top from y0 to y1.
// A jump in height between two pieces is a vertical step.
struct Piece { double x0, x1, y0, y1; };
const std::vector<Piece> level = {
{0.0, 1.0, 0.00, 0.00}, // flat
{1.0, 2.0, 0.20, 0.56}, // 0.2 step up, then a 20 degree ramp
{2.0, 3.0, 0.56, 0.00}, // crest, then a 29 degree slope down
{3.0, 4.0, -0.15, -0.15}, // a 0.15 stair down
{4.0, 5.0, 0.45, 0.45}, // a 0.6 ledge: too high to step
};
const Piece& pieceAt(double x) {
for (const Piece& p : level) if (x < p.x1) return p;
return level.back();
}
double groundY(double x) { const Piece& p = pieceAt(x); return p.y0 + (p.y1 - p.y0) * (x - p.x0) / (p.x1 - p.x0); }
double slopeOf(double x) { const Piece& p = pieceAt(x); return (p.y1 - p.y0) / (p.x1 - p.x0); }
void walk(bool snap) {
const double dt = 0.1, speed = 2.5, g = 9.81, maxStep = 0.3, snapDist = 0.3, maxSlope = std::tan(45 * 3.141592653589793 / 180);
double x = 0.1, y = 0, vy = 0;
bool grounded = true;
std::cout << (snap ? "with ground snap:\n" : "without ground snap:\n");
for (int step = 1; step <= 16; step++) {
double nx = x + speed * dt;
double ny = y + vy * dt;
if (!grounded) vy -= g * dt;
double gy = groundY(nx);
std::string what;
if (ny < gy) { // the move ends below the ground
double rise = gy - std::max(y, ny);
if (rise > maxStep || std::fabs(slopeOf(nx)) > maxSlope) { nx = x; ny = y; what = "blocked"; }
else { what = rise > 0.05 ? "step up" : (grounded ? "walk" : "land"); ny = gy; }
grounded = true;
} else if (grounded && snap && ny - gy <= snapDist) {
what = ny - gy > 0.05 ? "snap down" : "walk"; ny = gy;
} else if (ny - gy > 1e-9) {
what = grounded ? "LEAVE GROUND" : "air";
grounded = false;
} else what = "walk";
x = nx; y = ny;
if (grounded) vy = speed * slopeOf(x); // follow the slope while grounded
std::cout << " " << std::setw(2) << step << " x " << x << " y " << std::setw(6) << z0(y)
<< " ground " << std::setw(6) << z0(groundY(x)) << " " << what << "\n";
}
}
int main() {
std::cout << std::fixed << std::setprecision(2);
walk(false);
walk(true);
}without ground snap:
1 x 0.35 y 0.00 ground 0.00 walk
2 x 0.60 y 0.00 ground 0.00 walk
3 x 0.85 y 0.00 ground 0.00 walk
4 x 1.10 y 0.24 ground 0.24 step up
5 x 1.35 y 0.33 ground 0.33 walk
6 x 1.60 y 0.42 ground 0.42 walk
7 x 1.85 y 0.51 ground 0.51 walk
8 x 2.10 y 0.60 ground 0.50 LEAVE GROUND
9 x 2.35 y 0.69 ground 0.36 air
10 x 2.60 y 0.68 ground 0.22 air
11 x 2.85 y 0.57 ground 0.08 air
12 x 3.10 y 0.37 ground -0.15 air
13 x 3.35 y 0.07 ground -0.15 air
14 x 3.60 y -0.15 ground -0.15 land
15 x 3.85 y -0.15 ground -0.15 walk
16 x 3.85 y -0.15 ground -0.15 blocked
with ground snap:
1 x 0.35 y 0.00 ground 0.00 walk
2 x 0.60 y 0.00 ground 0.00 walk
3 x 0.85 y 0.00 ground 0.00 walk
4 x 1.10 y 0.24 ground 0.24 step up
5 x 1.35 y 0.33 ground 0.33 walk
6 x 1.60 y 0.42 ground 0.42 walk
7 x 1.85 y 0.51 ground 0.51 walk
8 x 2.10 y 0.50 ground 0.50 snap down
9 x 2.35 y 0.36 ground 0.36 walk
10 x 2.60 y 0.22 ground 0.22 walk
11 x 2.85 y 0.08 ground 0.08 walk
12 x 3.10 y -0.15 ground -0.15 snap down
13 x 3.35 y -0.15 ground -0.15 walk
14 x 3.60 y -0.15 ground -0.15 walk
15 x 3.85 y -0.15 ground -0.15 walk
16 x 3.85 y -0.15 ground -0.15 blocked
Follow the two runs:
- Step 4: both climb the 0.2 m step (
riseis belowmaxStep). - Steps 5–7: both follow the ramp, and the grounded character's vertical velocity matches the slope (
vy = speed * slope). - Step 8, the crest: without snapping the character keeps its upward velocity, leaves the ground and flies over the whole down slope and the stair, landing at step 14. With snapping, the ground is 0.10 m below, within the 0.3 m snap distance, so it sticks and walks down the slope.
- Step 12, the stair down: the snapping character drops 0.15 m onto the next stair without ever becoming airborne. That is what keeps footstep sounds, animations and "can jump" logic stable on staircases.
- Step 16: the 0.6 m ledge is higher than
maxStep, so the move is blocked.
The snap distance is a trade-off. Too short and the character still launches off crests at high speed. Too long and it sticks to the ground when it should fall off a real ledge, which looks like being pulled down a cliff.
Coyote time and other friends
Grounded state is input for gameplay, so a few more rules usually join the floor logic:
- Coyote time: allow a jump for about 0.1 s after leaving the ground, because players press jump a little late at ledges.
- Jump buffering: remember a jump press for about 0.1 s before landing.
- Slope speed: scale walk speed down on steep uphill slopes, or keep the horizontal speed constant (Godot's
floor_constant_speed). - Ledge perching: decide whether a capsule whose edge barely touches a ledge counts as standing on it (Unreal has perch settings for exactly this).
Test yourself
With a 45° slope limit, which test decides whether a contact normal n is walkable?
n.y > 45dot(n, up) >= cos(45°)dot(n, up) <= sin(45°)length(n) >= 0.707
What does ground snapping fix?
- Tunneling through thin walls
- The character launching off crests and bouncing down stairs, losing its grounded state
- Characters getting stuck on steps
- Sliding on steep slopes
How does a typical controller climb a step?
- It applies an upward impulse
- Sweep up by the step height, move forward, sweep down, and keep the result if it lands on walkable ground
- It increases the skin width
- It turns off collisions for a frame