How do I make this camera move towards mouse work with multiple resolutions ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By wildboar

I am making 2d game so I use mode 2d ascpect expand, and with those settings following code does not work with multiple window resolutions, but works perfect on default window resolution.

I tried adapting viewport to OS.get_real_window_size() or something, but it did not work.

    var viewport = get_viewport()
	var direction = viewport.get_mouse_position() - viewport.size / 2.0
	var percent = (direction / viewport.size * 2).length()
	var camera_position = Vector2()
	var max_zoom_distance = get_current_weapon().raycast.cast_to.x
		camera_position = global_position + direction.normalized() * max_zoom_distance * (percent / ZOOM_MAX_CAMERA_PERCENT)
		camera_position = global_position + direction.normalized() * max_zoom_distance
	camera.global_position = lerp(camera.global_position, camera_position, Game.zoom_sensitivity)

Found the formula originally from Move camera to mouse - Archive - Godot Forum

Goal is to move camera towards mouse and I call it “zoom” because its for shooter game.

Following kind of works, but it does not feel as good in the game since its not as consistent speed.

if camera.global_position.distance_to(global_position) < get_current_weapon().raycast.cast_to.x:
   var direction = get_global_mouse_position() - global_position
   camera.global_position = lerp(camera.global_position, direction, Game.zoom_sensitivity)

Anyways I do not know how to make it better than this :frowning:

Edit: does not work when moved around. sadly.

wildboar | 2021-03-05 09:41

:bust_in_silhouette: Reply From: wildboar
var mid_position = (global_position + get_global_mouse_position()) / 2
camera.global_position = camera.global_position.linear_interpolate(mid_position, Game.zoom_sensitivity * delta)

This seems to work fine, just not as smooth.