File size: 1,104 Bytes
8f07272
 
 
 
 
 
 
 
 
 
 
 
 
 
9dac9a3
8f07272
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
extends CharacterBody3D

@export var look_sensitivity: float = 0.005
@export var max_speed : float = 5.0
var is_controlled = false

func _ready():
	CameraManager.register_flycam(self)

func set_control(value):
	is_controlled = value
	$Camera3D.current = value
	
	
func _process(_delta):
	var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	direction.y =  Input.get_axis("cam_down", "cam_up")
	if direction:
		velocity.x = direction.x * max_speed
		velocity.y = direction.y * max_speed
		velocity.z = direction.z * max_speed
	else:
		velocity.x = move_toward(velocity.x, 0, max_speed)
		velocity.y = move_toward(velocity.y, 0, max_speed)
		velocity.z = move_toward(velocity.z, 0, max_speed)
	
	
	move_and_slide()
	
	
func _unhandled_input(event):
	if is_controlled and event is InputEventMouseMotion:
		rotate_y(-event.relative.x * look_sensitivity)
		$Camera3D.rotate_x(-event.relative.y * look_sensitivity)
		$Camera3D.rotation.x = clamp($Camera3D.rotation.x, -PI/2, PI/2)