Godot Platformer Lesson 1a: Player Movement =============================================== Summary -------- This lesson covers player jumps. Prerequisites -------------- * :doc:`Lesson 1a: Player Movement<1a-player-movement>` Video -------- .. raw:: html Resources ----------- Assets: https://github.com/GDQuest/godot-beginner-2d-platformer/releases/tag/1.1.0 Errata ----------- link: https://www.gdquest.com/tutorial/godot/2d/your-first-game/chapter/1-player-and-enemy/ For this video there is a value updated at 1:02:26 that is not mentioned in the video. Notes ----------- * In MacOS replace is accessed using command-f Code -------- Repository: https://github.com/los-alamos-steam-lab/GDQuest-Platformer/tree/1b-player-jump Actor Code ^^^^^^^^^^^^^^^^^^^^ .. code-block:: gdscript extends KinematicBody2D class_name Actor const FLOOR_NORMAL = Vector2.UP export var speed := Vector2(300, 1000) export var gravity := 4000.0 var _velocity := Vector2.ZERO Player Code ^^^^^^^^^^^^^^^^^^^^ .. code-block:: gdscript extends Actor func _physics_process(delta): var is_jump_interrupted: = Input.is_action_just_released("jump") and _velocity.y < 0 var direction := get_direction() _velocity = calculate_move_velocity(_velocity, direction, speed, is_jump_interrupted) _velocity = move_and_slide(_velocity, FLOOR_NORMAL) func get_direction() -> Vector2: return Vector2( Input.get_action_strength("move_right") - Input.get_action_strength("move_left"), -1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 0.0 ) func calculate_move_velocity(linear_velocity: Vector2, direction: Vector2, speed: Vector2, is_jump_interrupted: bool) -> Vector2: var new_velocity: = linear_velocity new_velocity.x = speed.x * direction.x new_velocity.y += gravity * get_physics_process_delta_time() if direction.y == -1: new_velocity.y = speed.y * direction.y if is_jump_interrupted: new_velocity.y = 0 return new_velocity Common Problems ------------------ * Player not jumping when on floor: * Sometimes :code:`is_on_floor()` can be a bit buggy. If you supply a second argument \ to :code:`move_and_slide()` then it will know which direction the floor (or rather the \ ceiling) is. So we want :code:`velocity = move_and_slide(velocity, Vector2.UP)`. This \ is covered in the video if you are patient, but it can seem like you made a mistake if \ you're testing your code as you go.