1 extends CharacterBody3D
6 @export_group("Movement")
7 @export var walk_speed := 3.0
8 @export var jog_speed := 6.0
9 @export var charge_speed := 20
11 @export var air_speed := 3.0
12 @export var acceleration := 20.0
13 @export var rotation_speed := 10.0
14 @export var idle_timeout := 5.0
15 @export var hard_landing_limit := 10.0
17 @export_group("Physics")
18 @export var push_force := 5.0
20 @export_group("Camera")
21 @export_range(1.0, 10.0) var camera_distance := 2.0
22 @export_range(0.0, 1.0) var mouse_sensitivity := 0.15
23 @export_range(0.0, 1.0) var mouse_sensitivity_x := 1.0
24 @export_range(0.0, 1.0) var mouse_sensitivity_y := 0.5
25 @export_range(0.0, 10.0) var joystick_sensitivity_x := 4.0
26 @export_range(0.0, 10.0) var joystick_sensitivity_y := 2.0
29 @onready var _debug: CanvasLayer = %debug
30 @onready var _camera_pivot: Node3D = %camera_pivot
31 @onready var _camera: Camera3D = %camera
32 @onready var _camera_spring: SpringArm3D = %spring
33 @onready var _skin: AnimatedSkin = %skin
34 @onready var _dust: GPUParticles3D = %dust
36 var _last_movement_direction := rotation
37 var _floor_normal := Vector3.ONE
38 var _ground_slope_input := 0.0
39 var _camera_input_direction := Vector2.ZERO
41 enum {CAMERA_MOUSE_INPUT, CAMERA_JOYSTICK_INPUT}
42 var _camera_input_method := CAMERA_MOUSE_INPUT
44 var _idle_time: float = 0.0
45 var _player_speed: float = 0
48 func _ready() -> void:
49 _debug.draw.add_vector(self, "velocity", 1, 1, Color(0,1,0,1))
50 _debug.draw.add_vector(self, "_floor_normal", 1, 1, Color(0, 0, 1, 1))
51 _debug.draw.add_vector(self, "_last_movement_direction", 1, 1, Color(1,0,0,1))
52 _debug.stats.add_property(self, "velocity", "length")
53 _debug.stats.add_property(self, "_idle_time", "round")
54 _debug.stats.add_property(self, "_ground_slope_input", "round")
56 _camera_spring.spring_length = camera_distance
57 _player_speed = jog_speed
61 func _physics_process(delta: float) -> void:
62 _process_camera(delta)
63 _process_player(delta)
66 func _unhandled_input(event: InputEvent) -> void:
67 # If user clicks on the window, capture the mouse and direct the camera with it
68 if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
71 #_camera_input_direction *= mouse_sensitivity
72 if event is InputEventMouseMotion:
73 _camera_input_method = CAMERA_MOUSE_INPUT
74 _camera_input_direction = event.screen_relative * mouse_sensitivity
75 elif event is InputEventJoypadMotion:
76 _camera_input_method = CAMERA_JOYSTICK_INPUT
77 _camera_input_direction = Input.get_vector("camera-left", "camera-right", "camera-up", "camera-down")
78 _camera_input_direction *= Vector2(joystick_sensitivity_x, -joystick_sensitivity_y)
81 func _input(event: InputEvent):
82 if event.is_action_pressed("player_run"):
83 _player_speed = walk_speed
84 elif event.is_action_released("player_run"):
85 _player_speed = jog_speed
87 if event.is_action_pressed("player_attack") and velocity.length() > jog_speed * .75:
88 _player_speed = charge_speed
89 elif event.is_action_released("player_attack"):
90 _player_speed = jog_speed
93 # Get the XZ input direction based on player's input relative to the camera
94 func _get_player_move_direction() -> Vector3:
95 var input_dir := Input.get_vector("player_left", "player_right", "player_forward", "player_backward")
96 var forward := _camera.global_basis.z
97 var right := _camera.global_basis.x
98 var move_direction := (forward * input_dir.y + right * input_dir.x).normalized()
100 return move_direction
103 func _process_camera(delta: float) -> void:
104 # vertical camera rotation
105 _camera_pivot.rotation.x += _camera_input_direction.y * mouse_sensitivity_y * delta
106 _camera_pivot.rotation.x = clamp(_camera_pivot.rotation.x, -PI / 6, PI / 3)
108 # horizontal camera rotation
109 _camera_pivot.rotation.y -= _camera_input_direction.x * mouse_sensitivity_x * delta
111 # reset mouse movement vector if mouse input
112 if _camera_input_method == CAMERA_MOUSE_INPUT:
113 _camera_input_direction = Vector2.ZERO
115 # change spring length depending on player speed
116 _camera_spring.spring_length = lerp(
117 _camera_spring.spring_length, camera_distance + velocity.length() / 4, delta
121 func _process_player_on_floor(delta: float):
122 var move_direction := _get_player_move_direction()
124 # if we're not stuck, then it's okay to set the velocity
125 _floor_normal = get_floor_normal()
126 _ground_slope_input = (PI / 2) - velocity.angle_to(_floor_normal)
127 velocity = velocity.move_toward(
128 move_direction * (_player_speed + _ground_slope_input * _player_speed),
131 var movement_speed := Vector3(velocity.x, 0, velocity.z).length()
133 # also, if we're moving, we're not idle
134 if move_direction.length() < 0.2:
135 if velocity == Vector3.ZERO:
138 _last_movement_direction = move_direction
141 # if camera is unlocked, rotate whole skin to face movement direction
142 var skin_target_angle := Vector3.BACK.signed_angle_to(_last_movement_direction, Vector3.UP)
143 _skin.global_rotation.y = lerp_angle(
144 _skin.global_rotation.y,
146 rotation_speed * delta
149 # lean into player momentum just a little bit
150 _skin.rotation.z = lerp_angle(
152 clamp(_last_movement_direction.signed_angle_to(velocity, Vector3.UP) * movement_speed * 0.08, -PI/4, PI/ 4),
153 rotation_speed * delta * 0.25
156 # let skin know how fast player is moving along the ground
157 _skin.set_grounded_speed(movement_speed)
159 # timescale tweaking for fun effect!
160 if _player_speed == charge_speed:
161 _skin.set_timescale(2.0)
164 _skin.set_timescale(1.0)
167 func _process_player(delta: float) -> void:
169 _process_player_on_floor(delta)
171 if _idle_time > idle_timeout:
176 _dust.emitting = velocity.length() > (0.75 * charge_speed) and is_on_floor()
177 _dust.amount_ratio = velocity.length()
180 velocity += get_gravity() * air_speed * delta
182 var prev_velocity := velocity
186 for i in get_slide_collision_count():
187 var c := get_slide_collision(i)
188 if c.get_collider() is RigidBody3D:
189 var col: RigidBody3D = c.get_collider()
190 col.apply_central_impulse(-c.get_normal() * prev_velocity.length() * push_force)