I have the following scene tree for my player:

They have a Camera2D as a child, which has a script attached to it. How can I make the camera zoom in and out as I pan on e.g. a MacBook trackpad?


1 Answer 1


You can zoom a camera in and out by handling the InputEventPanGesture input event:

extends Camera2D

var maximum_zoom_in = 0.15
var minimum_zoom_out = 4
var zoom_sensitivity = 0.01

func _unhandled_input(event):
    if event is InputEventPanGesture:
        var zoom_amount = event.delta.y * zoom_sensitivity
        var new_zoom = zoom.y + zoom_amount
        if (new_zoom < maximum_zoom_in):
            new_zoom = maximum_zoom_in
        elif (new_zoom > minimum_zoom_out):
            new_zoom = minimum_zoom_out
        zoom = Vector2(new_zoom, new_zoom)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .