func _input(event: InputEvent) -> void:
if event is InputEventScreenDrag:
_touch_points[event.index] = event.position
if _touch_points.size() == 1:
var new_position = -event.relative * touch_pan_speed
camera.position = (camera.position + new_position).clamp(_camera_boundaries.position, _camera_boundaries.end)
Pinch Zoom
## The amount the player can zoom in. 1.0 means they cannot zoom in, and
## anything higher allows zooming in.
@export_range(1.0, 5.0, 0.01) var zoom_in_max := 1.0
## The amount the player can zoom out. 1.0 means they cannot zoom out, and
## anything lower allows zooming out.
@export_range(0.001, 1.0, 0.01) var zoom_out_max := 0.5
var _touches := {}
var _start_distance := 0.0
func _input(event: InputEvent) -> void:
if not Engine.is_editor_hint():
if event is InputEventScreenTouch:
if event.pressed:
_touches[event.index] = event.position
else:
_touches.erase(event.index)
elif event is InputEventScreenDrag:
_touches[event.index] = event.position
_finger_count_check()
func _finger_count_check() -> void:
if _touches.size() == 2:
var fingers = _touches.values()
_pinch_zoom(fingers[0].distance_to(fingers[1]))
else:
_start_distance = 0.0
func _pinch_zoom(current_distance: float) -> void:
if _start_distance == 0.0:
_start_distance = current_distance
else:
var zoom_factor: float = current_distance / _start_distance
camera.zoom = clamp(camera.zoom * zoom_factor, Vector2.ONE * zoom_in_max, Vector2.ONE * zoom_out_max)
Full code in my Camera2D Plugin. I haven’t gotten time to put it into my Camera3D Plugin yet. There I just have a switch camera button to switch between the camera options I want to have available.