basic_movement.gd 1.0 KB

123456789101112131415161718192021222324252627282930313233
  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. # Get the gravity from the project settings to be synced with RigidBody nodes.
  6. var gravity: float = ProjectSettings.get_setting("physics/3d/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 input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
  17. var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  18. if direction:
  19. velocity.x = direction.x * SPEED
  20. velocity.z = direction.z * SPEED
  21. else:
  22. velocity.x = move_toward(velocity.x, 0, SPEED)
  23. velocity.z = move_toward(velocity.z, 0, SPEED)
  24. move_and_slide()