forked from JucanAndreiDaniel/GothicGodot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfree_look_camera.gd
45 lines (37 loc) · 1.69 KB
/
free_look_camera.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#Copyright © 2022 Marc Nahr: https://github.com/MarcPhi/godot-free-look-camera
extends Camera3D
@export_range(0, 10, 0.01) var sensitivity : float = 3
@export_range(0, 1000, 0.1) var default_velocity : float = 5
@export_range(0, 10, 0.01) var speed_scale : float = 1.17
@export_range(1, 100, 0.1) var boost_speed_multiplier : float = 3.0
@export var max_speed : float = 1000
@export var min_speed : float = 0.2
@onready var _velocity = default_velocity
func _input(event):
if not current:
return
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
if event is InputEventMouseMotion:
rotation.y -= event.relative.x / 1000 * sensitivity
rotation.x -= event.relative.y / 1000 * sensitivity
rotation.x = clamp(rotation.x, PI/-2, PI/2)
if event is InputEventMouseButton:
match event.button_index:
MOUSE_BUTTON_RIGHT:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED if event.pressed else Input.MOUSE_MODE_VISIBLE)
MOUSE_BUTTON_WHEEL_UP: # increase fly velocity
_velocity = clamp(_velocity * speed_scale, min_speed, max_speed)
MOUSE_BUTTON_WHEEL_DOWN: # decrease fly velocity
_velocity = clamp(_velocity / speed_scale, min_speed, max_speed)
func _process(delta):
if not current:
return
var direction = Vector3(
float(Input.is_physical_key_pressed(KEY_D)) - float(Input.is_physical_key_pressed(KEY_A)),
float(Input.is_physical_key_pressed(KEY_E)) - float(Input.is_physical_key_pressed(KEY_Q)),
float(Input.is_physical_key_pressed(KEY_S)) - float(Input.is_physical_key_pressed(KEY_W))
).normalized()
if Input.is_physical_key_pressed(KEY_SHIFT): # boost
translate(direction * _velocity * delta * boost_speed_multiplier)
else:
translate(direction * _velocity * delta)