2-finger control (and zoom) for mobile game

Godot Version



I can’t get any further with the controls. Unfortunately, my control system doesn’t work very well. Sometimes the camera doesn’t like the way I want it to. The controls are simply not as fluid and functional as you would expect from mobile games.

Do any of you have a script that you can give me?

Here is my script:

extends Camera2D

@export var min_zoom := 0.25
@export var max_zoom := 1.0
@export var zoom_rate := 8.0
@export var zoom_delta := 0.1
@export var drag_speed := 1.0

@onready var target_zoom: float = min_zoom
@onready var hud := $HUD as HUD

func _physics_process(delta: float) → void:
zoom.x = lerp(zoom.x, target_zoom, zoom_rate * delta)
zoom.y = lerp(zoom.y, target_zoom, zoom_rate * delta)

func _unhandled_input(event: InputEvent) → void:
# Cell phone
if event is InputEventPanGesture:
var current_zoom:float = zoom_delta * zoom_delta
if event.delta.x > 0:
target_zoom = min(target_zoom + current_zoom, max_zoom)
elif event.delta.x < 0:
target_zoom = max(target_zoom - current_zoom, min_zoom)
if event is InputEventScreenDrag:
if event.relative.length_squared() > 0:
position -= event.relative * drag_speed / zoom

# PC
if event is InputEventMouseButton:
	if event.button_index == MOUSE_BUTTON_WHEEL_UP:
		target_zoom = min(target_zoom + zoom_delta, max_zoom)
	if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
		target_zoom = max(target_zoom - zoom_delta, min_zoom)
	if event.pressed:
		if event.button_index == MOUSE_BUTTON_LEFT:
	elif event.button_index == MOUSE_BUTTON_LEFT: