Godot Version
4.2.2
Question
I am trying to create a camera zoom effect that can zoom in and out in a 3d space based on the mouse position. My camera will remain stationary. My first thought was to do something like the following,
func _input(event):
#Zoom the camera in
if event is InputEventKey and Input.is_key_pressed(KEY_Z):
var mouse_pos: Vector3 = camera.project_position(get_viewport().get_mouse_position(), 2.89)
# stupid godot syntax cant do x < y < z
if camera.fov > 10 and (-2.5 < mouse_pos.x and mouse_pos.x < 2.5) and (-1.25 < mouse_pos.y and mouse_pos.y < 1.25):
camera.set_h_offset(camera.get_h_offset() + VELOCITY * (mouse_pos.x - camera.get_h_offset()))
camera.set_v_offset(camera.get_v_offset() + VELOCITY * (mouse_pos.y - camera.get_v_offset()))
camera.fov = camera.fov - DELTA_FOV
#Zoom the camera out
if event is InputEventKey and Input.is_key_pressed(KEY_X):
var mouse_pos: Vector3 = camera.project_position(get_viewport().get_mouse_position(), 2.89)
if camera.fov < 80 and (-2.5 < mouse_pos.x and mouse_pos.x < 2.5) and (-1.25 < mouse_pos.y and mouse_pos.y < 1.25):
camera.set_h_offset(camera.get_h_offset() + VELOCITY * (mouse_pos.x - camera.get_h_offset()))
camera.set_v_offset(camera.get_v_offset() + VELOCITY * (mouse_pos.y - camera.get_v_offset()))
camera.fov = camera.fov + DELTA_FOV
however I don’t want the camera perspective to change and changing the offset for the camera seems equivalent to just translating the camera which is not what I want to do.
I know that this is a good use case for frustum camera so I tried a similar approach by adjusting the frustum offset instead, unfortunately, it seems that camera.project_position breaks down for frustum camera (Camera3D project_* methods not accounting for frustum offset · Issue #61174 · godotengine/godot · GitHub).
Surely there is a way to get the camera to zoom without shifting the perspective. I really only need it to work on a 5x2.5 plane mesh centered at the origin.
Thank you for your help