49 lines
1.3 KiB
GDScript
49 lines
1.3 KiB
GDScript
extends CharacterBody3D
|
|
|
|
@export var speed := 3.0
|
|
@export var gravity := 9.8
|
|
@export var jump_velocity := 4.0
|
|
@export var mouse_sensitivity := 0.2
|
|
|
|
var x_rot := 0.0 # vertical camera rotation
|
|
|
|
func _physics_process(delta):
|
|
# --- Movement Input ---
|
|
var input_dir = Vector3.ZERO
|
|
if Input.is_action_pressed("xr_move_forward"):
|
|
input_dir.z -= 1
|
|
if Input.is_action_pressed("xr_move_backward"):
|
|
input_dir.z += 1
|
|
if Input.is_action_pressed("xr_move_left"):
|
|
input_dir.x -= 1
|
|
if Input.is_action_pressed("xr_move_right"):
|
|
input_dir.x += 1
|
|
input_dir = input_dir.normalized()
|
|
|
|
# Transform direction to global
|
|
var direction = transform.basis * input_dir
|
|
velocity.x = direction.x * speed
|
|
velocity.z = direction.z * speed
|
|
|
|
# --- Gravity ---
|
|
if not is_on_floor():
|
|
velocity.y -= gravity * delta
|
|
else:
|
|
velocity.y = 0
|
|
if Input.is_action_just_pressed("xr_jump"):
|
|
velocity.y = jump_velocity
|
|
|
|
# --- Move Character ---
|
|
move_and_slide()
|
|
|
|
func _unhandled_input(event):
|
|
if event is InputEventMouseMotion:
|
|
# Rotate the body horizontally
|
|
rotate_y(deg_to_rad(-event.relative.x * mouse_sensitivity))
|
|
|
|
# Rotate camera vertically
|
|
var camera = $XRCamera3D
|
|
x_rot += -event.relative.y * mouse_sensitivity
|
|
x_rot = clamp(x_rot, -90, 90)
|
|
camera.rotation.x = deg_to_rad(x_rot)
|