basic_movement.gd 703 B

123456789101112131415161718192021222324252627
  1. # meta-description: Classic movement for gravity games (platformer, ...)
  2. extends _BASE_
  3. const SPEED = 300.0
  4. const JUMP_VELOCITY = -400.0
  5. func _physics_process(delta: float) -> void:
  6. # Add the gravity.
  7. if not is_on_floor():
  8. velocity += get_gravity() * delta
  9. # Handle jump.
  10. if Input.is_action_just_pressed("ui_accept") and is_on_floor():
  11. velocity.y = JUMP_VELOCITY
  12. # Get the input direction and handle the movement/deceleration.
  13. # As good practice, you should replace UI actions with custom gameplay actions.
  14. var direction := Input.get_axis("ui_left", "ui_right")
  15. if direction:
  16. velocity.x = direction * SPEED
  17. else:
  18. velocity.x = move_toward(velocity.x, 0, SPEED)
  19. move_and_slide()