How to zoom camera to mouse

Godot Version

4.2.1

Question

Hi, in drawing software you can zoom the camera to the mouse. This means that if you have your mouse on a certain pixel, after zooming, the mouse will still be on that pixel. How do I implement this in Godot? I tried using the solution in Camera2D zoom position towards the mouse but it didn’t work. It seems to zoom into the bottom right corner (which is 0,0).

func set_zoom(delta: Vector2) -> void:
	$Camera2D.zoom += delta
	$Camera2D.global_position += (-0.5 * SCREEN_SIZE + get_global_mouse_position()) * -delta
1 Like

That’s weird, it was weirdly easy. I have no clue what the other thread’s solution is for.

func set_zoom(delta: Vector2) -> void:
	var mouse_pos := get_global_mouse_position()
	$Camera2D.zoom += delta
	var new_mouse_pos := get_global_mouse_position()
	$Camera2D.position += mouse_pos - new_mouse_pos
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.