extends CharacterBody3D const SPEED = 5.0 const CAMERA_ROTATE_RATE = PI / 12 # rotate camera func _unhandled_input(event: InputEvent) -> void: if event.is_action("camera-rotate-left"): $cameraPivot.rotate_y(CAMERA_ROTATE_RATE) elif event.is_action("camera-rotate-right"): $cameraPivot.rotate_y(-CAMERA_ROTATE_RATE) func _physics_process(delta: float) -> void: # Add the gravity. if not is_on_floor(): velocity += get_gravity() * delta # Get the input direction and handle the movement/deceleration. var input_dir := Input.get_vector("player-turn-left", "player-turn-right", "player-forward", "player-backward") var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() direction = direction.rotated(Vector3.UP, $cameraPivot.rotation.y) if direction: velocity.x = direction.x * SPEED velocity.z = direction.z * SPEED # rotate the character to face movement direction $player.rotation.y = lerp_angle($player.rotation.y, atan2(-velocity.x, -velocity.z), 0.15) else: velocity.x = move_toward(velocity.x, 0, SPEED) velocity.z = move_toward(velocity.z, 0, SPEED) move_and_slide()