-
Notifications
You must be signed in to change notification settings - Fork 6
/
Camera.gd
47 lines (35 loc) · 1.7 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
extends Camera3D
# Adapted from:
# https://github.com/nekotogd/Raytracing_Godot4
@export var mouse_sensitivity : float = 0.5
@export var move_speed : float = 5.0
@export var roll_speed: float = 40.0
func _input(event):
if event is InputEventMouseMotion:
if Input.is_action_pressed("left_mouse_btn"):
rotate_y(deg_to_rad(event.relative.x * mouse_sensitivity))
rotate_object_local(Vector3(1.0, 0.0, 0.0), deg_to_rad(event.relative.y * mouse_sensitivity))
func _process(_delta):
if Input.is_action_pressed("left_mouse_btn"):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if Input.is_action_pressed("roll_cw"):
rotate_object_local(Vector3(0.0, 0.0, 1.0), deg_to_rad(-roll_speed * _delta))
if Input.is_action_pressed("roll_ccw"):
rotate_object_local(Vector3(0.0, 0.0, 1.0), deg_to_rad(roll_speed * _delta))
_move(_delta)
func _move(_delta):
var input_vector := Vector3.ZERO
input_vector.x = Input.get_action_strength("move_left") - Input.get_action_strength("move_right")
input_vector.z = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
input_vector.y = Input.get_action_strength("move_up") - Input.get_action_strength("move_down")
if input_vector.length() > 1.0:
input_vector = input_vector.normalized()
var displacement := Vector3.ZERO
displacement = global_transform.basis.z * move_speed * input_vector.z * _delta
global_transform.origin += displacement
displacement = global_transform.basis.x * move_speed * input_vector.x * _delta
global_transform.origin += displacement
displacement = global_transform.basis.y * move_speed * input_vector.y * _delta
global_transform.origin -= displacement