CHALLENGE 1: A Player That Stops at Walls ==================================================================== TASK ---- Build a CharacterBody2D with a CollisionShape2D (a real shape assigned), plus a couple of static wall nodes with their own collision shapes nearby. Attach the four-directional move_and_slide() controller from this chapter and confirm the character stops at the walls instead of passing through. SCENE STRUCTURE ---------------- Root (Node2D) |-- Player (CharacterBody2D) | |-- Sprite2D | |-- CollisionShape2D (Shape: RectangleShape2D, sized to the sprite) |-- Wall1 (StaticBody2D) | |-- CollisionShape2D (Shape: RectangleShape2D) |-- Wall2 (StaticBody2D) |-- CollisionShape2D (Shape: RectangleShape2D) (StaticBody2D is the node type used for immovable scenery like walls and floors - it participates in collision but, unlike RigidBody2D, never moves on its own.) SOLUTION CODE (attached to Player) -------------------------------------- extends CharacterBody2D const SPEED = 200.0 func _physics_process(delta: float) -> void: var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down") velocity = direction * SPEED move_and_slide() TESTING IT ---------- Run the scene and move the Player toward Wall1 or Wall2 using the arrow keys / WASD. The character should stop right at the wall's edge - it can slide along the wall if approaching at an angle, but it can never pass through it. WHY THIS WORKS AS AN ANSWER ---------------------------- Because Player extends CharacterBody2D (not Sprite2D directly, as in earlier chapters), setting velocity and calling move_and_slide() does real, collision-aware movement instead of blindly changing position. move_and_slide() checks the Player's own CollisionShape2D against every other collision shape in the scene - including Wall1's and Wall2's - and stops (or slides along) the movement the moment it would overlap one of them. This only works because every node involved has a real Shape assigned to its own CollisionShape2D, per this chapter's own warning - an empty CollisionShape2D on either the Player or a wall would make that node invisible to collision entirely, and the character would pass straight through as if the wall weren't there.