How do I stop the camera from moving faster when I zoom in

Godot Version

Godot 4.2.2

Question

Help Programming General
As the title says, I need help in making the camera move slower based on the zoom.
Here is a video that describes my problem a little better than I did:

This is ALL the code used for the camera (36 lines)

extends Camera2D

var _target_zoom: float = 1.0

const MIN_ZOOM: float = 1.5
const MAX_ZOOM: float = 4
const ZOOM_INCREMENT: float = 0.2

const ZOOM_RATE: float = 8.0

func _ready() -> void: #At the start of the program, sets the camera to the min zoom
	zoom_out()

func _physics_process(delta: float) -> void: #Zooms in with a smooth animation
	zoom = lerp(zoom, _target_zoom * Vector2.ONE, ZOOM_RATE * delta)
	set_physics_process(not is_equal_approx(zoom.x, _target_zoom))
	
func zoom_in() -> void: #Zooms in and makes sure that it doesnt go over the limit
	_target_zoom = min(_target_zoom + ZOOM_INCREMENT, MAX_ZOOM)
	set_physics_process(true)
	
func zoom_out() -> void: #Zooms out and makes sure that it doesnt go over the limit
	_target_zoom = max(_target_zoom - ZOOM_INCREMENT, MIN_ZOOM)
	set_physics_process(true)

func _unhandled_input(event: InputEvent) -> void: #Gets inputs that dont get handled by buttons
	if event is InputEventMouseMotion:
		if event.button_mask == MOUSE_BUTTON_MASK_LEFT:
			if Input.is_action_pressed("drag"):
				position -= event.relative * zoom #moves the camera in the opposite direction of the mouse to make the dragging effect
	if event is InputEventMouseButton:
		if event.is_pressed():
			if event.button_index == MOUSE_BUTTON_WHEEL_UP: #If scroll up, zoom
				zoom_in()
			if event.button_index == MOUSE_BUTTON_WHEEL_DOWN: #If scroll down, dezoom
				zoom_out()

Any help is appreciated, Thanks in advance.

1 Like

I’m too tired to work out the math right now, but I think you need to change the position -= event.relative ... line to adjust event.relative so that it gets smaller as the zoom increases.

Think about it this way: when the zoom is at neutral, the multiplier for event.relative is 1. When the view is zoomed so that you can only see half as much on say, the X axis, event.relative should be multiplied by 0.5.

Actually… what happens if you just stop multiplying it with zoom? So that the line is just position -= event.relative.

I hope I’m making some sense, I figured some help is better than none. :sweat_smile:

1 Like

Thanks to your comment, I started testing on that line and I found out that the position -= event.relative * zoom should have been position -= event.relative / zoom.
Actually not joking I wouldn’t have found the solution without the hint

2 Likes

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