player_controller.gd 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. if global_position.y < -30.0:
  23. print('fell off world, fixing position')
  24. global_position = Vector3(1.0, 5.0, 1.0)
  25. func _physics_process(delta):
  26. var phys_norm = MC_DELTA / delta
  27. # Apply momentum adjustment
  28. # var real_slip = surface_slipperyness if is_on_floor() else 1.0
  29. # var fric_mult = pow(MOMENTUM_BASE_MULT * real_slip, delta / MC_DELTA)
  30. # velocity.x *= fric_mult
  31. # velocity.z *= fric_mult
  32. #print("fric_mult ", fric_mult)
  33. # Vertical velocity and jumping.
  34. # if is_on_floor():
  35. # if Input.is_action_pressed("move_jump") and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
  36. # print("jump")
  37. # vel_y = jump_vel / phys_norm
  38. # else:
  39. # vel_y = 0
  40. # else:
  41. # # Apply vertical movement math
  42. # vel_y = (vel_y - (GRAVITY * 1)) * pow(AIR_DRAG_MULT, phys_norm)
  43. #print("yvel ", vel_y)
  44. # Update velocity for player input.
  45. # if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
  46. # var input_vec = Input.get_vector("move_left", "move_right", "move_forward", "move_backward").normalized()
  47. # var ixz = input_vec.x * global_transform.basis.x + input_vec.y * global_transform.basis.z
  48. # var ixzn = ixz.normalized()
  49. # var base_mult = GROUND_ACCEL_BASE_MULT if is_on_floor() else AIR_ACCEL_BASE_MULT
  50. # var slip_mult = pow(0.6 / real_slip, 3 * phys_norm) if is_on_floor() else 1.0
  51. # velocity += ixzn * base_mult * slip_mult * MOVE_MULT * phys_norm
  52. # Apply movement
  53. # velocity.y = vel_y
  54. # was_on_floor = is_on_floor()
  55. # move_and_slide()
  56. # Mouse capture.
  57. if Input.is_action_just_pressed("change_mouse_input"):
  58. Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if Input.mouse_mode == Input.MOUSE_MODE_VISIBLE else Input.MOUSE_MODE_VISIBLE
  59. func _input(event):
  60. if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
  61. rotate_y(-event.relative.x * look_sensitivity)
  62. camera.rotate_x(-event.relative.y * look_sensitivity)
  63. camera.rotation.x = clamp(camera.rotation.x, -PI/2, PI/2)