Skip to content

Commit 86962d3

Browse files
committed
add FOV control to freelook camera
1 parent 37836c1 commit 86962d3

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

addons/free_camera.gd

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ class_name FreelookCamera3D extends Camera3D
22

33
@export var movement_speed := 10
44
@export var mouse_sensitivity := 0.006
5+
@export var fov_change_speed := 50.0
56

67
var _previous_camera: Camera3D
78
var _camera_input_direction := Vector2.ZERO
8-
9+
var _target_fov: float
910

1011
func _ready() -> void:
1112
current = false
1213
process_mode = PROCESS_MODE_ALWAYS
1314
set_process(current)
14-
15+
_target_fov = fov
1516

1617
func _input(event: InputEvent) -> void:
1718
if event.is_action_pressed("toggle_freelook_camera"):
@@ -25,6 +26,11 @@ func _input(event: InputEvent) -> void:
2526
if is_camera_motion:
2627
_camera_input_direction = event.relative * mouse_sensitivity
2728

29+
if event is InputEventMouseButton:
30+
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
31+
_target_fov = max(_target_fov - 1, 1)
32+
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
33+
_target_fov = min(_target_fov + 1, 179)
2834

2935
func _process(delta: float) -> void:
3036
var movement := Vector3.ZERO
@@ -49,11 +55,13 @@ func _process(delta: float) -> void:
4955

5056
_camera_input_direction = Vector2.ZERO
5157

58+
fov = move_toward(fov, _target_fov, delta * fov_change_speed)
5259

5360
func _toggle_camera_mode() -> void:
5461
if not current:
5562
_previous_camera = get_viewport().get_camera_3d()
5663
fov = _previous_camera.fov
64+
_target_fov = fov
5765
global_transform = _previous_camera.global_transform
5866
make_current()
5967
else:

0 commit comments

Comments
 (0)