walker.gd 916 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. class_name Walker
  2. extends CharacterBody3D
  3. # How fast the player moves in meters per second.
  4. @export var speed = 14
  5. # The downward acceleration when in the air, in meters per second squared.
  6. @export var fall_acceleration = 75
  7. # Vertical impulse applied to the character upon jumping in meters per second.
  8. @export var jump_impulse = 20
  9. var target_velocity = Vector3.ZERO
  10. func trigger_jump() -> void:
  11. if is_on_floor():
  12. target_velocity.y = jump_impulse
  13. func trigger_direction(dir: Vector2) -> void:
  14. target_velocity.x = dir.x * speed
  15. target_velocity.z = dir.y * speed
  16. func _physics_process(delta: float) -> void:
  17. # Gravity
  18. if not is_on_floor():
  19. target_velocity.y = target_velocity.y - (fall_acceleration * delta)
  20. # Moving the Character
  21. velocity = target_velocity
  22. move_and_slide()
  23. func _on_dir_changed(dir: Vector2) -> void:
  24. trigger_direction(dir)
  25. func _on_main_action() -> void:
  26. trigger_jump()