OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / scenes / test / dc_terrain_pt_flycam.gd

42 lines · gd ref:
extends Camera3D

@export var move_speed: float = 30.0
@export var boost_mult: float = 4.0
@export var mouse_sens: float = 0.002

var _yaw: float = 0.0
var _pitch: float = 0.0
var _captured: bool = false

func _ready() -> void:
	_yaw = rotation.y
	_pitch = rotation.x
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	_captured = true

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseMotion and _captured:
		_yaw -= event.relative.x * mouse_sens
		_pitch -= event.relative.y * mouse_sens
		_pitch = clamp(_pitch, -PI/2 + 0.01, PI/2 - 0.01)
		var b := Basis()
		b = b.rotated(Vector3.UP, _yaw)
		b = b.rotated(b.x, _pitch)
		transform.basis = b
	elif event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE:
		_captured = not _captured
		Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if _captured else Input.MOUSE_MODE_VISIBLE

func _process(delta: float) -> void:
	var dir := Vector3.ZERO
	if Input.is_key_pressed(KEY_W): dir -= transform.basis.z
	if Input.is_key_pressed(KEY_S): dir += transform.basis.z
	if Input.is_key_pressed(KEY_A): dir -= transform.basis.x
	if Input.is_key_pressed(KEY_D): dir += transform.basis.x
	if Input.is_key_pressed(KEY_Q): dir -= Vector3.UP
	if Input.is_key_pressed(KEY_E): dir += Vector3.UP
	if dir.length_squared() > 0.0:
		dir = dir.normalized()
		var spd := move_speed * (boost_mult if Input.is_key_pressed(KEY_SHIFT) else 1.0)
		global_position += dir * spd * delta