feat: add scrollwheel camera zoom
Mouse wheel up/down smoothly zooms the camera in and out. Zoom range: close-in overview (5 high, 4 back) to wide survey (16 high, 12 back). Default sits at midpoint (10 high, 8 back) matching the previous fixed offset. Configurable via min_zoom_offset, max_zoom_offset, and zoom_step exports. Zoom interpolates with the same follow_speed as position tracking for consistent feel.
This commit is contained in:
+18
-2
@@ -12,9 +12,16 @@ extends Node3D
|
|||||||
@export var deadzone_right := 3.0
|
@export var deadzone_right := 3.0
|
||||||
@export var deadzone_forward := 3.0
|
@export var deadzone_forward := 3.0
|
||||||
|
|
||||||
|
@export var min_zoom_offset := Vector3(0, 5, 4)
|
||||||
|
@export var max_zoom_offset := Vector3(0, 16, 12)
|
||||||
|
@export var zoom_step := 0.1
|
||||||
|
|
||||||
var yaw := 0.0
|
var yaw := 0.0
|
||||||
var smoothed_yaw := 0.0
|
var smoothed_yaw := 0.0
|
||||||
|
|
||||||
|
var zoom := 0.5
|
||||||
|
var zoom_smooth := 0.5
|
||||||
|
|
||||||
var focus_position := Vector3.ZERO
|
var focus_position := Vector3.ZERO
|
||||||
var desired_focus_position := Vector3.ZERO
|
var desired_focus_position := Vector3.ZERO
|
||||||
|
|
||||||
@@ -25,13 +32,19 @@ func _ready() -> void:
|
|||||||
if target:
|
if target:
|
||||||
focus_position = target.global_position
|
focus_position = target.global_position
|
||||||
desired_focus_position = focus_position
|
desired_focus_position = focus_position
|
||||||
global_position = focus_position + follow_offset
|
global_position = focus_position + min_zoom_offset.lerp(max_zoom_offset, zoom_smooth)
|
||||||
|
|
||||||
|
|
||||||
func _unhandled_input(event: InputEvent) -> void:
|
func _unhandled_input(event: InputEvent) -> void:
|
||||||
if event is InputEventMouseMotion:
|
if event is InputEventMouseMotion:
|
||||||
yaw -= event.relative.x * mouse_sensitivity
|
yaw -= event.relative.x * mouse_sensitivity
|
||||||
|
|
||||||
|
if event is InputEventMouseButton:
|
||||||
|
if event.button_index == MOUSE_BUTTON_WHEEL_UP and event.pressed:
|
||||||
|
zoom = maxf(zoom - zoom_step, 0.0)
|
||||||
|
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN and event.pressed:
|
||||||
|
zoom = minf(zoom + zoom_step, 1.0)
|
||||||
|
|
||||||
if event.is_action_pressed("ui_cancel"):
|
if event.is_action_pressed("ui_cancel"):
|
||||||
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
||||||
|
|
||||||
@@ -67,7 +80,10 @@ func _physics_process(delta: float) -> void:
|
|||||||
|
|
||||||
focus_position = focus_position.lerp(desired_focus_position, focus_t)
|
focus_position = focus_position.lerp(desired_focus_position, focus_t)
|
||||||
|
|
||||||
var rotated_offset := follow_offset.rotated(Vector3.UP, smoothed_yaw)
|
zoom_smooth = lerpf(zoom_smooth, zoom, follow_t)
|
||||||
|
var active_offset := min_zoom_offset.lerp(max_zoom_offset, zoom_smooth)
|
||||||
|
|
||||||
|
var rotated_offset := active_offset.rotated(Vector3.UP, smoothed_yaw)
|
||||||
var desired_position := focus_position + rotated_offset
|
var desired_position := focus_position + rotated_offset
|
||||||
|
|
||||||
global_position = global_position.lerp(desired_position, follow_t)
|
global_position = global_position.lerp(desired_position, follow_t)
|
||||||
|
|||||||
Reference in New Issue
Block a user