Menu
CoddyTech

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 icon

Challenge

Easy

Make the walls stop the hero.

  1. Replace the position.x line with one that sets velocity.x to direction * speed, with no delta.
  2. Under it, call move_and_slide().

Press Play and walk both ways: the pillar and the wall stop the hero. Then Check.

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

All lessons in Input and Movement

Practice on your own: Godot playground