Walls Stop You
Part of the Input and Movement section of Coddy's Godot journey. Lesson 9 of 28.
The walls and the pillar are StaticBody2D nodes. A static body never moves. Each has a CollisionShape2D for its solid shape and a ColorRect to draw it.
Only move_and_slide() checks for walls. Changing position moves the body without checking, so it goes through walls:
position.x += direction * speed * delta # through walls
velocity.x = direction * speed # stopped by walls
move_and_slide()Text after # is a comment. Godot skips it.
A body that hits a wall at an angle keeps the part of its movement along the wall. It slides instead of sticking, which gives move_and_slide() its name.
Challenge
EasyThis hero walks through the pillar and the walls. Move it with velocity and move_and_slide() so the walls stop it.
Try it yourself
extends CharacterBody2D
var speed = 200
func _physics_process(delta):
var direction = Input.get_axis("move_left", "move_right")
position.x += direction * speed * delta
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground