Pinch-to-Zoom Camera

Smooth pinch-to-zoom camera control for mobile devices supporting both orthographic and perspective cameras. Features configurable zoom limits and optional smoothing.

File
PinchToZoomCamera.cs
Unity
2022.3+
Level
intermediate
Size
3.0 KB · 119 lines
Added
2026-03-23
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

What it adds to your GameObject

A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 6 serialized fields with their defaults.

Inspector
Script PinchToZoomCamera
Zoom Settings
Zoom Speed 0.5
Min Zoom 2
Max Zoom 20
Smoothing
Enable Smoothing
Smooth Speed 8
Camera Mode
Auto Detect Mode

When to use it

Perfect for mobile strategy games, RTS, puzzle games, and map views that need intuitive zoom controls. Use for any mobile game where players need to zoom in and out of a 2D or 3D scene — tower defense overviews, photo viewers, or level editors.

Setup

  1. Attach the PinchToZoomCamera script to your Main Camera
  2. Set minZoom and maxZoom limits in the inspector
  3. Enable autoDetectMode to automatically configure for orthographic or perspective
  4. Adjust zoomSpeed and smoothSpeed to taste
  5. Test with two-finger pinch on a mobile device or use Remote in the editor

Features

  • Smooth pinch-to-zoom
  • Configurable zoom limits
  • Works with orthographic and perspective cameras
  • Zoom speed multiplier
  • Optional zoom smoothing

Inspector fields

Every field Pinch-to-Zoom Camera exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Zoom SpeedzoomSpeedfloat0.5Zoom Settings
Min ZoomminZoomfloat2Zoom Settings
Max ZoommaxZoomfloat20Zoom Settings
Enable SmoothingenableSmoothingbooltrueSmoothing
Smooth SpeedsmoothSpeedfloat8Smoothing
Auto Detect ModeautoDetectModebooltrueCamera Mode

Public API

What other scripts can call on PinchToZoomCamera.

MemberReturns
SetZoom(float zoom)void
ResetZoom()void
GetCurrentZoom()float
GetZoomNormalized()float

Requirements

  • Reads the legacy Input Manager (UnityEngine.Input). If Project Settings > Player > Active Input Handling is set to Input System Package only, Unity throws an InvalidOperationException; set it to Both.

Common mistakes

If autoDetectMode is enabled, it overrides your minZoom/maxZoom values on Awake — disable it when you need custom limits. The zoom feels different on orthographic (changes size) vs perspective (changes FOV) cameras; for perspective cameras that move forward instead of changing FOV, you'll need to modify ApplyZoom. Mouse scroll fallback uses a 100x multiplier that may need adjustment for your scroll wheel sensitivity.

Source code

PinchToZoomCamera.cs, 119 lines, MIT licensed. Paste it into a new C# file of the same name.

PinchToZoomCamera.cs
C#
using UnityEngine;

public class PinchToZoomCamera : MonoBehaviour
{
    [Header("Zoom Settings")]
    [SerializeField] private float zoomSpeed = 0.5f;
    [SerializeField] private float minZoom = 2f;
    [SerializeField] private float maxZoom = 20f;

    [Header("Smoothing")]
    [SerializeField] private bool enableSmoothing = true;
    [SerializeField] private float smoothSpeed = 8f;

    [Header("Camera Mode")]
    [SerializeField] private bool autoDetectMode = true;

    private Camera cam;
    private float targetZoom;
    private float previousTouchDistance;
    private bool isPinching;

    private void Awake()
    {
        cam = GetComponent<Camera>();
        if (cam == null)
            cam = Camera.main;

        if (autoDetectMode)
        {
            minZoom = cam.orthographic ? 1f : 20f;
            maxZoom = cam.orthographic ? 30f : 100f;
        }

        targetZoom = cam.orthographic ? cam.orthographicSize : cam.fieldOfView;
    }

    private void Update()
    {
        HandlePinchInput();
        ApplyZoom();
    }

    private void HandlePinchInput()
    {
        if (Input.touchCount == 2)
        {
            Touch touch0 = Input.GetTouch(0);
            Touch touch1 = Input.GetTouch(1);

            float currentDistance = Vector2.Distance(touch0.position, touch1.position);

            if (!isPinching)
            {
                isPinching = true;
                previousTouchDistance = currentDistance;
                return;
            }

            float delta = previousTouchDistance - currentDistance;
            float zoomDelta = delta * zoomSpeed * Time.deltaTime;

            targetZoom += zoomDelta;
            targetZoom = Mathf.Clamp(targetZoom, minZoom, maxZoom);

            previousTouchDistance = currentDistance;
        }
        else
        {
            isPinching = false;
        }

        // Mouse scroll fallback for editor testing
        float scroll = Input.GetAxis("Mouse ScrollWheel");
        if (Mathf.Abs(scroll) > 0.01f)
        {
            targetZoom -= scroll * zoomSpeed * 100f * Time.deltaTime;
            targetZoom = Mathf.Clamp(targetZoom, minZoom, maxZoom);
        }
    }

    private void ApplyZoom()
    {
        if (cam.orthographic)
        {
            if (enableSmoothing)
                cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetZoom, Time.deltaTime * smoothSpeed);
            else
                cam.orthographicSize = targetZoom;
        }
        else
        {
            if (enableSmoothing)
                cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, targetZoom, Time.deltaTime * smoothSpeed);
            else
                cam.fieldOfView = targetZoom;
        }
    }

    public void SetZoom(float zoom)
    {
        targetZoom = Mathf.Clamp(zoom, minZoom, maxZoom);
    }

    public void ResetZoom()
    {
        targetZoom = cam.orthographic ? 5f : 60f;
    }

    public float GetCurrentZoom()
    {
        return cam.orthographic ? cam.orthographicSize : cam.fieldOfView;
    }

    public float GetZoomNormalized()
    {
        float current = GetCurrentZoom();
        return Mathf.InverseLerp(minZoom, maxZoom, current);
    }
}

Next step

Gyroscope Camera ControllerRotates the camera with the device gyroscope, with smoothing, sensitivity, calibration, axis locking and a fallback for devices without a gyroscope.