35 lines
1.2 KiB
GDScript
35 lines
1.2 KiB
GDScript
## Free-fly camera: WASD + QE up/down, RMB hold to look, Shift to speed up
|
|
extends Camera3D
|
|
|
|
@export var speed : float = 200.0
|
|
@export var fast_mult : float = 5.0
|
|
@export var sensitivity: float = 0.003
|
|
|
|
var _captured := false
|
|
|
|
func _ready() -> void:
|
|
current = true
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton:
|
|
if event.button_index == MOUSE_BUTTON_RIGHT:
|
|
_captured = event.pressed
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if _captured else Input.MOUSE_MODE_VISIBLE
|
|
|
|
if _captured and event is InputEventMouseMotion:
|
|
rotate_y(-event.relative.x * sensitivity)
|
|
rotate_object_local(Vector3.RIGHT, -event.relative.y * sensitivity)
|
|
|
|
func _process(delta: float) -> void:
|
|
if not _captured:
|
|
return
|
|
var move := Vector3.ZERO
|
|
if Input.is_key_pressed(KEY_W): move -= basis.z
|
|
if Input.is_key_pressed(KEY_S): move += basis.z
|
|
if Input.is_key_pressed(KEY_A): move -= basis.x
|
|
if Input.is_key_pressed(KEY_D): move += basis.x
|
|
if Input.is_key_pressed(KEY_E): move += Vector3.UP
|
|
if Input.is_key_pressed(KEY_Q): move -= Vector3.UP
|
|
var s := speed * (fast_mult if Input.is_key_pressed(KEY_SHIFT) else 1.0)
|
|
position += move.normalized() * s * delta
|