Let's break down the difference between velocity vs position, and how move_and_slide() and move_and_collide() affect them:
1. velocity vs position
-
position: The exact coordinates (x, y) of your object in the world.
- Changing
position teleports the object instantly to that spot.
- Example:
position.x += 10 moves the object 10 pixels right immediately.
-
velocity: The speed and direction of movement (pixels/second).
- Changing
velocity tells the object how fast to move, not where to be.
- The engine calculates the new
position based on velocity * delta (time passed).
- Example:
velocity.x = 100 means "move 100 pixels right every second".
Think of it like this:
position is like saying "Be here NOW!"
velocity is like saying "Go this fast, and I'll figure out where you end up"
2. Moving Without move_and_* functions
If you just modify position or velocity directly:
# Method 1: Direct position change (BAD for physics)
position.x += 10
# Method 2: Velocity-based movement (BETTER, but ignores walls)
velocity.x = 100
position.x += velocity.x * delta
Problems:
- Objects can clip through walls: If you move 100 pixels and there's a wall 50 pixels away, you'll be on the other side of it.
- No collision response: You won't bounce, stop, or slide; you'll just phase through.
3. move_and_collide()
var collision = move_and_collide(velocity * delta)
What it does:
- Moves the object by
velocity * delta
- Stops exactly at the first collision it hits
- Returns a
KinematicCollision2D object with info about the collision (if any)
Example behavior:
- You're moving right at 100px/s, hit a wall → Stop exactly at the wall.
- No sliding, no bouncing; just hard stop.
4. move_and_slide()
velocity = move_and_slide(velocity)
What it does:
- Moves based on
velocity
- Slides along walls if you hit them at an angle
- Automatically adjusts
velocity to prevent getting stuck
- Great for platformers, top-down games, or any smooth movement
Example behavior:
- Running right, hit a wall at 45° → Slide up the wall.
- Walking into a corner → Slide along one wall, then the other.
Key differences:
| move_and_collide() | move_and_slide() |
|---|
| Stops at walls | Hard stop, no sliding | Slides along surfaces |
| Velocity handling | You manage velocity | Function updates velocity for you |
| Use case | Precise collision detection | Smooth character movement (platformers) |
When to use each:
move_and_collide(): When you need exact collision points (e.g., bullets, precise mechanics).
move_and_slide(): For player movement where you want natural sliding (platformers, 3D walkers).
Remember: Both functions must be used with velocity, not direct position changes. They handle the physics and collision for you!
Here's a quick code comparison:
# BAD (no collision)
velocity.x = 100
position.x += velocity.x * delta
# OK (stops at walls, no sliding)
var collision = move_and_collide(velocity * delta)
if collision:
print("Hit something!")
# BEST (smooth player movement)
velocity.x = 100
velocity = move_and_slide(velocity)
Now you know why pros use velocity + move_and_slide() for characters and move_and_collide() for projectiles!