quack/player.gd

51 lines
1.6 KiB
GDScript

extends CharacterBody3D
@export var speed: float = 5.0
@export var jump_velocity = 4.5
@export var mouse_sensitivity: float = 6.0
@export var acceleration: float = 5.0
@onready var head: Node3D = %Head
@onready var camera: Camera3D = %Camera
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta: float) -> void:
if !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("move_left", "move_right", "move_forward", "move_back")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
var temp_vel := velocity
temp_vel.y = 0
var target := direction
target *= speed
temp_vel = temp_vel.lerp(target, acceleration * delta)
velocity.x = temp_vel.x
velocity.z = temp_vel.z
move_and_slide()
func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
head.rotation.x -= event.relative.y * mouse_sensitivity * 0.001
head.rotation.x = clamp(head.rotation.x, -PI/2.0 + 0.05, PI/2.0 - 0.05)
rotation.y -= event.relative.x * mouse_sensitivity * 0.001
rotation.y = wrapf(rotation.y, 0.0, TAU)
if event.is_action_pressed("ui_cancel"):
var cm = Input.get_mouse_mode()
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE if cm == Input.MOUSE_MODE_CAPTURED else Input.MOUSE_MODE_CAPTURED)