1 extends CharacterBody3D
5 const CAMERA_ROTATE_RATE = PI / 6
9 func _unhandled_input(event: InputEvent) -> void:
10 if event.is_action_pressed("camera-rotate-left"):
11 $cameraPivot.rotate_y(CAMERA_ROTATE_RATE)
12 elif event.is_action_pressed("camera-rotate-right"):
13 $cameraPivot.rotate_y(-CAMERA_ROTATE_RATE)
16 func _physics_process(delta: float) -> void:
19 velocity += get_gravity() * delta
21 # Get the input direction and handle the movement/deceleration.
22 var input_dir := Input.get_vector("player-turn-left", "player-turn-right", "player-forward", "player-backward")
23 var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
24 direction = direction.rotated(Vector3.UP, $cameraPivot.rotation.y)
27 velocity.x = direction.x * SPEED
28 velocity.z = direction.z * SPEED
30 # rotate the character to face movement direction
31 $player.rotation.y = lerp_angle($player.rotation.y, atan2(-velocity.x, -velocity.z), 0.15)
33 velocity.x = move_toward(velocity.x, 0, SPEED)
34 velocity.z = move_toward(velocity.z, 0, SPEED)