⚡ Physics Bodies Guide
Understanding Godot's physics system - for Unity developers
🏃 CharacterBody2D
Use for: Player characters, NPCs, enemies🔷 Unity Equivalent:
Like a Rigidbody2D with CharacterController behavior. You control
movement directly, but get collision detection.
The Two Movement Methods
move_and_slide()
Automatically slides along collision surfaces. Best for most games.
- Handles slopes
- Provides
is_on_floor(),is_on_wall() - Modifies
velocityfor you
move_and_collide()
Stops on collision, returns collision info. Best for custom physics.
- Returns
KinematicCollision2D - You handle the response
- Good for bouncing, custom sliding
Platformer Movement (Complete Example)
platformer.gd
12345678910111213141516171819202122
extends CharacterBody2D
var speed = 300.0
var jump_force = -400.0
# Get gravity from project settings
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
# Apply gravity every frame
velocity.y += gravity * delta
# Get horizontal input (-1, 0, or 1)
var direction = Input.get_axis("move_left", "move_right")
velocity.x = direction * speed
# Jump only when on floor
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_force
# Move and handle collisions
move_and_slide()Top-Down Movement
top_down.gd
12345678910111213
extends CharacterBody2D
var speed = 200.0
func _physics_process(delta):
# Get direction from all 4 movement keys
var direction = Input.get_vector(
"move_left", "move_right",
"move_up", "move_down"
)
velocity = direction * speed
move_and_slide()💡 Pro Tips
- Always use
_physics_process()for movement, not_process() is_on_floor()only works after callingmove_and_slide()- Set
floor_max_angleto control what counts as a "floor"