CHALLENGE 3: A Full Player Controller That Faces Its Direction ==================================================================== TASK ---- Build the full four-directional player controller from this chapter. Extend it so that whenever direction.x is negative, the sprite's flip_h (from Chapter 4) is set to true, and false whenever it's positive - so the character visually faces the direction it's moving. SETUP ----- Input Map actions: move_left, move_right, move_up, move_down, each bound to the arrow keys (or WASD). SOLUTION CODE (attached to the Sprite2D) -------------------------------------------- extends Sprite2D const SPEED = 200.0 func _physics_process(delta: float) -> void: var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down") position += direction * SPEED * delta if direction.x < 0: flip_h = true elif direction.x > 0: flip_h = false WHY THIS WORKS AS AN ANSWER ---------------------------- The movement half is unchanged from the chapter's own player controller: Input.get_vector() returns a normalized direction from the four named actions, and position += direction * SPEED * delta applies it every physics frame. The facing logic is a straightforward if/elif check on direction.x, the horizontal component of that same Vector2 - if it's negative, the player is moving left, so flip_h is set true to mirror the sprite; if it's positive, moving right, so flip_h is set false to show it unflipped. Deliberately using if/elif rather than if/else here matters: when direction.x is exactly 0 (moving purely up, down, or not moving at all), neither branch runs, which correctly leaves flip_h exactly as it already was - the character keeps facing whichever way it was last facing horizontally, rather than snapping back to a default orientation every time it moves purely vertically.