godot-2d-movement

Implement 2D kinematic character movement in Godot 4.7 with CharacterBody2D and move_and_slide(): platformer run/jump with gravity, top-down 8-direction motion, slope handling, and reading collisions. Use when coding a 2D player or enemy controller, a platformer or top-down character, or fixing move

By gamedev-skills · 2,374 installs

npx skills add gamedev-skills/awesome-gamedev-agent-skills --skill godot-2d-movement

Source repository · Upstream listing

Godot 2D Movement (4.x) Build responsive 2D character controllers with CharacterBody2D and the argument less move and slide() . Targets Godot 4.7 . When to use Use when scripting a 2D player/enemy that moves and collides: platformer (gravity + jump) or top down (free 8 direction), slope walking, or wall/floor detection. Use when move and slide() "does nothing", the character sinks through floors, or is on floor() is always false. When not to use: dynamic rigid bodies, areas, raycasts, collision layers → godot physics ; tile based levels → godot tilemap ; full platformer game template → the platformer genre skill; engine agnostic feel tuning → physics tuning . Core workflow 1. Use CharacterBody2D with a CollisionShape2D child. It is script driven: it does not fall or react to forces on its own. 2. Set the velocity property, then call move and slide() (no arguments in 4.x). The method reads velocity , moves the body, slides along surfaces, and updates velocity to reflect what actually happened. 3. Do it in physics process(delta) — move and slide() uses the physics step's delta internally, so do not multiply velocity by delta yourself. 4. Platformer: add gravity to velocity.y each tick, jump by setting velocity.y negative when is on floor() . 5. Top down: build a direction from input and scale by speed; set motion mode = MOTION MODE FLOATING so there is no "floor/wall" distinction. 6. Read results with is on floor() , is on wall() , get wall normal() , and get slide collision(i) after the call. Patterns 1. Platformer controller (gravity, jump, slopes) A fixed gravity export keeps tuning simple. To follow the project setting ( Physics 2D Default Gravity ) and any Area2D gravity overrides instead, use velocity += get gravity() delta , as the built in CharacterBody2D script template does. 2. Top down 8 direction movement 3. Reading slide collisions after moving 4. Coyote time (forgiving jump just after leaving a ledge) Pitfalls 3.x signature is gone. move and slide(velocity) (returning the new velocity) was removed. In 4.x set the velocity property and call move and slide() with no args. move and slide(velocity, Vector2.UP) will not parse. Multiplying velocity by delta. move and slide() already accounts for the physics delta. Setting velocity = dir speed delta makes the body crawl. Movement in process instead of physics process causes frame rate dependent, jittery motion. Always move in physics process . is on floor() always false when up direction is wrong (default Vector2.UP ), there is no CollisionShape2D , or you never called move and slide() this frame. Sinking through floors usually means the collision shape is missing/zero sized, the floor body is on a layer this body's mask doesn't include, or you moved the body via position += instead of velocity + move and slide() . Slope slipping while idle: keep floor stop on slope = true (default) and raise floor snap length (default 1 px) so the body sticks to downward slopes. References For one way platforms, moving platforms, jump buffering, variable jump height, and move and collide() (manual collision response), read references/controller recipes.md . Related skills godot physics — collision layers/masks, areas, raycasts, rigid bodies. godot tilemap — building the levels this character walks on. godot animation — driving sprite/skeletal animation from movement state. camera systems — follow camera, deadzone, and look ahead that track this character. platformer / input systems — full genre template and rebindable input.