44 lines
1.3 KiB
GDScript
44 lines
1.3 KiB
GDScript
extends CharacterBody3D
|
|
|
|
@export var speed = 5.0
|
|
@export var sprint_speed = 10.0
|
|
@export var jump_velocity = 4.5
|
|
@export var mouse_sensitivity = 0.002
|
|
|
|
@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("ui_accept") and is_on_floor():
|
|
velocity.y = jump_velocity
|
|
|
|
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
|
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
|
|
|
if direction:
|
|
var current_speed = sprint_speed if Input.is_key_pressed(KEY_SHIFT) else speed
|
|
velocity.x = direction.x * current_speed
|
|
velocity.z = direction.z * current_speed
|
|
|
|
# Rotate player to face movement direction
|
|
var look_dir = Vector3(direction.x, 0, direction.z)
|
|
if look_dir.length() > 0:
|
|
look_at(global_position + look_dir, Vector3.UP)
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, speed)
|
|
velocity.z = move_toward(velocity.z, 0, speed)
|
|
|
|
move_and_slide() |