extends CharacterBody3D
@export var speed = 14.0
@export var rotation_speed = 4.0
var cam_spring_arm # Reference to your SpringArm node
func _ready():
cam_spring_arm = get_node("SpringArm3D") # Or however you access it
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) # Optional: Capture mouse for FPS-like controls
func _physics_process(delta):
# Movement
var input_vector = Input.get_vector("move_left", "move_right", "move_backward", "move_forward")
var velocity = Vector3.ZERO
if input_vector != Vector2.ZERO:
velocity = (transform.basis * Vector3(input_vector.x, 0, input_vector.y)).normalized() * speed
#Rotation
var aim_vector = Input.get_vector("aim_left", "aim_right", "aim_down", "aim_up")
if aim_vector != Vector2.ZERO:
rotation.y -= aim_vector.x * rotation_speed * delta
cam_spring_arm.rotation.x -= aim_vector.y * rotation_speed * delta
cam_spring_arm.rotation.x = clamp(cam_spring_arm.rotation.x, deg_to_rad(-75), deg_to_rad(75))
velocity.y -= 9.81 * delta # Gravity
# Apply movement
move_and_slide()
func _input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event is InputEventKey and event.keycode == KEY_ESCAPE and event.is_pressed():
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)