basic_movement.gd 892 B

123456789101112131415161718192021222324252627282930
  1. # meta-description: Classic movement for gravity games (FPS, TPS, ...)
  2. extends _BASE_
  3. const SPEED = 5.0
  4. const JUMP_VELOCITY = 4.5
  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 input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
  15. var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  16. if direction:
  17. velocity.x = direction.x * SPEED
  18. velocity.z = direction.z * SPEED
  19. else:
  20. velocity.x = move_toward(velocity.x, 0, SPEED)
  21. velocity.z = move_toward(velocity.z, 0, SPEED)
  22. move_and_slide()