Files
skyprincess/scripts/player_controller.gd
FoxSpellCaster 49b518bd4e added controls
controlls are backwards though :P
2025-12-08 09:31:52 -05:00

52 lines
1.6 KiB
GDScript

extends CharacterBody3D
@export var speed = 5.0
@export var sprint_speed = 10.0
@export var crouch_speed = 2.0
@export var jump_velocity = 4.5
@export var mouse_sensitivity = 0.002
@export var turn_speed = 10.0
@onready var camera = $Camera3D
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _input(event):
if event is InputEventMouseMotion:
rotate_y(-event.relative.x * mouse_sensitivity)
camera.rotate_x(-event.relative.y * mouse_sensitivity)
camera.rotation.x = clamp(camera.rotation.x, -PI/2, PI/2)
func _physics_process(delta):
if not is_on_floor():
velocity.y -= gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
var input_dir = Input.get_vector("slideLeft", "slideRight", "backward", "forward")
input_dir = -input_dir
var direction = (camera.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
var current_speed = speed
if Input.is_action_pressed("sprint"):
current_speed = sprint_speed
elif Input.is_action_pressed("crouch"):
current_speed = crouch_speed
velocity.x = direction.x * current_speed
velocity.z = direction.z * current_speed
# Smoothly rotate player to face movement direction only if moving forward/backward
if input_dir.y != 0:
var target_angle = atan2(direction.x, direction.z)
rotation.y = lerp_angle(rotation.y, target_angle, delta * turn_speed)
else:
velocity.x = move_toward(velocity.x, 0, speed)
velocity.z = move_toward(velocity.z, 0, speed)
move_and_slide()