Floor snap pulls a character back down onto the floor after each move, up to a set distance: 12 px here. It's what keeps a running character on a downhill ramp instead of hopping down it.
| Running at 340 px/s down a 45° ramp, 120 ticks/s | Per tick |
|---|---|
| The floor drops away | 2.8 px |
| Gravity pulls the character down | about 0.2 px |
| Floor snap can close | 12 px (Godot's default: 1 px) |
Why a running character bounces down slopes
On a 45° ramp the floor drops as far as you move sideways, and gravity, starting from zero at the top, covers only a fraction of a pixel of that. After a single tick there's a gap under the feet and the game thinks you're in the air. The character hops, the animation flickers, and a jump press only works inside coyote time.
Floor snap: back onto the ramp after every move
After each move, floor snap looks down by up to its length and puts the character back on the floor it finds. It's skipped while the character moves up, so jumps work as usual, and it's far shorter than a 40 px tile, so walking off a real ledge still means falling.
How to implement floor snap on slopes
- Snap after moving, never while rising. If the character was on the floor and isn't moving up, pull it down onto the floor below, up to the snap length.
- Size it from your top ground speed. Each tick the floor drops speed × tan(slope angle) ÷ physics ticks per second. Beat that, and stay shorter than your smallest real ledge.
- Keep ground speed constant on slopes, so the character doesn't slow down uphill or speed up downhill.
- Set the max floor angle a little above your steepest ramp.
The code in Godot 4
floor_snap_length = 12.0 # px de «pegado»
floor_constant_speed = true # misma velocidad en rampa
floor_stop_on_slope = true # no resbalar parado
floor_max_angle = deg_to_rad(50) All four are built into CharacterBody2D, whose defaults are 1 px of snap and constant speed off.
Quick answers
How long should floor snap be?
More than the floor drops in one tick at your top ground speed, less than your smallest real ledge. Here that's 12 px: four times the 2.8 px a 45° ramp drops per tick at full run, and under a third of a 40 px tile.
What floor_max_angle should I use for 45° slopes?
Something above 45°, because 45° is exactly Godot's default limit. This demo uses 0.87 rad, about 50°.
Discussion
Comments are hosted on GitHub Discussions — sign in with GitHub to reply.