52 lines
1.5 KiB
GDScript
52 lines
1.5 KiB
GDScript
extends CharacterBody3D
|
|
|
|
@export var rotation_speed: float = 0.002
|
|
|
|
|
|
const SPEED = 5.0
|
|
const JUMP_VELOCITY = 4.5
|
|
|
|
func _ready() -> void:
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if Input.is_action_pressed("ui_cancel"):
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
|
|
if event is InputEventMouseButton and event.button_index == 1 and event.is_pressed():
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
|
|
if event is InputEventMouseMotion:
|
|
handle_motion(event)
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
# Add the gravity.
|
|
if not is_on_floor():
|
|
velocity += get_gravity() * delta
|
|
|
|
# Handle jump.
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
|
var input_dir := Input.get_vector("left", "right", "front", "back")
|
|
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
|
if direction:
|
|
velocity.x = direction.x * SPEED
|
|
velocity.z = direction.z * SPEED
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
velocity.z = move_toward(velocity.z, 0, SPEED)
|
|
|
|
move_and_slide()
|
|
|
|
func handle_motion(event: InputEventMouseMotion) -> void:
|
|
if not Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
|
|
return
|
|
|
|
$Camera3D.rotation.x -= event.relative.y * rotation_speed
|
|
$Camera3D.rotation.x = clamp($Camera3D.rotation.x, deg_to_rad(-89), deg_to_rad(89))
|
|
rotation.y -= event.relative.x * rotation_speed
|