0 votes

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.getrealwindow_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
    if percent < ZOOM_MAX_CAMERA_PERCENT:
        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 https://godotengine.org/qa/95641/move-camera-to-mouse ...

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

in Engine by (87 points)

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 :(

Edit: does not work when moved around. sadly.

1 Answer

0 votes
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.

by (87 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.