Excellent question. This gets to the heart of how physics and movement work in Godot.
Let's break it down with a simple analogy:
position: Where you are right now.
velocity: The direction and speed you want to go.
Scenario 1: Without move_and_slide or move_and_collide
In this scenario, you are manually changing the character's position. You are basically a ghost.
You calculate a desired velocity and then directly add it to your position.
# GHOST MODE - NO COLLISION
func _physics_process(delta):
var direction = Input.get_axis("ui_left", "ui_right")
var speed = 300
# 1. Set your desired velocity
velocity.x = direction * speed
# 2. Directly change your position
# You are teleporting a tiny amount each frame.
position += velocity * delta
The Relationship:
velocity is just a temporary container for your intended movement.
- You are directly telling Godot, "Move my
position to this new spot."
The HUGE Problem:
The character will pass straight through walls, floors, and other objects. Godot's physics engine is completely ignored because you are just changing coordinate values. You are the "ghost."
Scenario 2: With move_and_slide or move_and_collide
In this scenario, you are no longer a ghost. You are a physical object that respects the laws of the game world.
You are not directly setting your position. Instead, you tell the physics engine your intention to move (your velocity), and the engine figures out where you should actually end up.
Using move_and_slide (Most Common)
move_and_slide is the best choice for standard player controllers. It handles collisions and sliding automatically.
# PHYSICAL MODE - WITH COLLISION AND SLIDING
func _physics_process(delta):
var direction = Input.get_axis("ui_left", "ui_right")
var speed = 300
var gravity = 980
# 1. Apply inputs and physics to your velocity
velocity.x = direction * speed
velocity.y += gravity * delta # Apply gravity
# 2. Ask the physics engine to move you
# You give it your desired velocity.
# It moves you, stops you at walls, and makes you slide.
# It then GIVES YOU BACK the new, correct velocity.
velocity = move_and_slide()
The Relationship:
- You update
velocity: You declare your intent (e.g., "I want to move right at 300 pixels/sec and fall due to gravity").
- You call
move_and_slide(): You hand this velocity over to the physics engine.
- The engine moves the
position: It moves your character's position as far as it can without hitting anything. If it hits a wall, it stops. If it hits a slope, it slides.
- The engine updates and returns
velocity: This is crucial. If you run into a wall, move_and_slide() will set velocity.x to 0 and give it back to you. If you land on the floor, it will set velocity.y to 0. You store this returned value so that in the next frame, you don't keep trying to build up speed into a wall.
Comparison Table
| Feature | Without move_and_slide | With move_and_slide | With move_and_collide |
|---|
| How it Works | Manually add velocity to position. | Hands velocity to the physics engine to handle movement and collisions. | Like move_and_slide, but stops at the first collision and doesn't slide. |
| Collision | None. You are a ghost. | Handled automatically. You stop at walls and slide along them. | Stops on contact. You get detailed info about what you hit. |
Role of velocity | A simple variable to calculate the next position. | Your intent to move. It's both an input and an output of the function. | Your intent to move. It's the input for the function. |
Role of position | You change it directly. | The physics engine changes it for you. | The physics engine changes it for you. |
| Best For | Menus, UI elements, things that shouldn't interact with the world. | Standard player characters, NPCs, any object that needs to move and collide smoothly. | Bouncing projectiles, checking if a specific spot can be reached, custom physics responses. |
In short:
- Without
move_and_slide: You say, "Teleport me here."
- With
move_and_slide: You say, "I want to go this way. Please move me there, but don't let me go through walls."