player_controller.gd 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. extends CharacterBody3D
  2. const MC_DELTA: float = 1.0 / 20.0
  3. const GROUND_ACCEL_BASE_MULT = 0.55
  4. const AIR_ACCEL_BASE_MULT = 0.05
  5. const MOMENTUM_BASE_MULT = 0.91
  6. const MOVE_MULT = 1
  7. const GRAVITY = 0.6
  8. const AIR_DRAG_MULT = 0.98
  9. @export var jump_vel: float = 15
  10. @export var stop_speed: float = 0.003
  11. var look_sensitivity = ProjectSettings.get_setting("player/look_sensitivity")
  12. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
  13. var surface_slipperyness = 0.6
  14. var vel_y = 0
  15. var was_on_floor = false
  16. @onready var camera: Camera3D = $Camera3D
  17. # Called when the node enters the scene tree for the first time.
  18. func _ready():
  19. pass # Replace with function body.
  20. # Called every frame. 'delta' is the elapsed time since the previous frame.
  21. func _process(_delta):
  22. pass
  23. # if global_position.y < -30.0:
  24. # print('fell off world, fixing position')
  25. # global_position = Vector3(1.0, 5.0, 1.0)
  26. func _physics_process(delta):
  27. var phys_norm = MC_DELTA / delta
  28. # Apply momentum adjustment
  29. # var real_slip = surface_slipperyness if is_on_floor() else 1.0
  30. # var fric_mult = pow(MOMENTUM_BASE_MULT * real_slip, delta / MC_DELTA)
  31. # velocity.x *= fric_mult
  32. # velocity.z *= fric_mult
  33. #print("fric_mult ", fric_mult)
  34. # Vertical velocity and jumping.
  35. # if is_on_floor():
  36. # if Input.is_action_pressed("move_jump") and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
  37. # print("jump")
  38. # vel_y = jump_vel / phys_norm
  39. # else:
  40. # vel_y = 0
  41. # else:
  42. # # Apply vertical movement math
  43. # vel_y = (vel_y - (GRAVITY * 1)) * pow(AIR_DRAG_MULT, phys_norm)
  44. #print("yvel ", vel_y)
  45. # Update velocity for player input.
  46. # if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
  47. # var input_vec = Input.get_vector("move_left", "move_right", "move_forward", "move_backward").normalized()
  48. # var ixz = input_vec.x * global_transform.basis.x + input_vec.y * global_transform.basis.z
  49. # var ixzn = ixz.normalized()
  50. # var base_mult = GROUND_ACCEL_BASE_MULT if is_on_floor() else AIR_ACCEL_BASE_MULT
  51. # var slip_mult = pow(0.6 / real_slip, 3 * phys_norm) if is_on_floor() else 1.0
  52. # velocity += ixzn * base_mult * slip_mult * MOVE_MULT * phys_norm
  53. # Apply movement
  54. # velocity.y = vel_y
  55. # was_on_floor = is_on_floor()
  56. # move_and_slide()
  57. # Mouse capture.
  58. if Input.is_action_just_pressed("change_mouse_input"):
  59. Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if Input.mouse_mode == Input.MOUSE_MODE_VISIBLE else Input.MOUSE_MODE_VISIBLE
  60. func _input(event):
  61. if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
  62. rotate_y(-event.relative.x * look_sensitivity)
  63. camera.rotate_x(-event.relative.y * look_sensitivity)
  64. camera.rotation.x = clamp(camera.rotation.x, -PI/2, PI/2)