-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcamera.gd
78 lines (55 loc) · 2.58 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Camera freelook and movement script
#
# Copyright © 2020-present Hugo Locurcio and contributors - MIT License
# See `LICENSE.md` included in the source distribution for details.
extends Camera3D
const MOUSE_SENSITIVITY = 0.0005
# The camera movement speed (tweakable using the mouse wheel).
var move_speed := 0.1
# Stores where the camera is wanting to go (based on pressed keys and speed modifier).
var motion := Vector3()
# Stores the effective camera velocity.
var velocity := Vector3()
# The initial camera node rotation.
var initial_rotation := rotation.y
# The rotation to lerp to (for mouse smoothing).
var rotation_dest := rotation
func _ready() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event: InputEvent) -> void:
# Mouse look (effective only if the mouse is captured).
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
# Horizontal mouse look.
rotation_dest.y -= event.relative.x * MOUSE_SENSITIVITY
# Vertical mouse look, clamped to -90..90 degrees.
rotation_dest.x = clamp(rotation_dest.x - event.relative.y * MOUSE_SENSITIVITY, deg_to_rad(-90), deg_to_rad(90))
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
if event.is_action_pressed("toggle_mouse_capture"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if event.is_action_pressed("movement_speed_increase"):
move_speed = min(1.5, move_speed + 0.1)
if event.is_action_pressed("movement_speed_decrease"):
move_speed = max(0.1, move_speed - 0.1)
func _process(delta: float) -> void:
if Input.is_action_pressed("ui_cancel"):
get_tree().quit()
rotation = rotation.lerp(rotation_dest, 0.1)
motion.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
motion.y = Input.get_action_strength("move_up") - Input.get_action_strength("move_down")
motion.z = Input.get_action_strength("move_back") - Input.get_action_strength("move_forward")
# Normalize motion
# (prevents diagonal movement from being `sqrt(2)` times faster than straight movement).
motion = motion.normalized()
# Speed modifier
if Input.is_action_pressed("move_speed"):
motion *= 2
# Transform the motion by the camera's transform.
motion = transform.basis.x * motion.x + transform.basis.y * motion.y + transform.basis.z * motion.z
# Add motion, apply friction and velocity.
velocity += motion * move_speed
velocity *= 0.98
position += velocity * delta
func _exit_tree() -> void:
# Restore the mouse cursor upon quitting.
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)