CHALLENGE 2: Gravity, Jumping, and Preventing a Double Jump ==================================================================== TASK ---- Add gravity and jumping to a CharacterBody2D on a static floor, using is_on_floor() to only allow jumping while grounded. Confirm the character can't "double jump" by holding the jump key in mid-air. SETUP ----- - A CharacterBody2D "Player" with a CollisionShape2D (real Shape assigned). - A StaticBody2D "Floor" beneath it, with its own CollisionShape2D. - An Input Map action "jump" bound to the spacebar. SOLUTION CODE (attached to Player) -------------------------------------- extends CharacterBody2D const GRAVITY = 900.0 const JUMP_VELOCITY = -400.0 func _physics_process(delta: float) -> void: if not is_on_floor(): velocity.y += GRAVITY * delta if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = JUMP_VELOCITY move_and_slide() WHY THIS WORKS AS AN ANSWER ---------------------------- This is the chapter's own gravity/jump example, and it satisfies the "no double jump" requirement for two separate reasons working together: 1. Input.is_action_just_pressed("jump") - not is_action_pressed() - only returns true for the single frame the spacebar is first pressed down (Chapter 5's own distinction). Holding the key down doesn't keep re-triggering it. 2. Even if is_action_just_pressed() somehow fired again mid-air, the "and is_on_floor()" condition would block it - is_on_floor() only returns true while the CharacterBody2D is actually resting on the Floor's own collision shape. The instant the jump happens, velocity.y becomes negative (moving up), the character leaves the floor, is_on_floor() becomes false, and the jump condition can no longer be satisfied at all until gravity brings the character back down and move_and_slide() registers it touching the floor again. Together, these two checks mean a jump can only ever be triggered by a fresh press while grounded - exactly the "no double jump" behavior the task asks for, with no extra flag variable needed the way Chapter 4's "print once" challenge required.