basic_movement.gd 856 B

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