Final Project: Player Movement Script
Combine everything from this course into a real, playable script: 2D platformer-style player movement, the foundation almost every 2D Godot game starts from.
Requirements
- Create a script extending
CharacterBody2Dwith@exportvariables forspeedandjump_velocity, tunable from the editor's Inspector. - In
_physics_process(delta), read left/right input and set horizontal velocity accordingly (useInput.get_axis("ui_left", "ui_right")or your own custom input actions). - Apply gravity to vertical velocity every frame, and let the player jump (set a negative vertical velocity) only when
is_on_floor()is true, so jumping mid-air isn't possible. - Call
move_and_slide()to actually apply the computed velocity and handle collisions. - Define a
signal health_depletedand a simplehealthvariable with atake_damage(amount)method that emits the signal when health reaches 0, connect to it from a separate script (even a simple one that just prints "Game Over") to prove the decoupling works.
Stretch goals
- Add a
Sprite2Dreference and flip it horizontally based on movement direction. - Add a double-jump, allowed only once per airborne period.
- Add a
@export var coyote_time: floatgrace period that still allows a jump for a few frames after walking off a platform edge.
Submit a link to your finished project below, an instructor will review it before you can mark this lesson complete. Good luck!