How to zoom and pan when you "pinch/swipe across screen" (iPhone)

I know how to zoom in when you hit an input key but how would you zoom when you pinch?

Also how would you pan to the side when you do a two finger swipe?

First you check that you have 2 touches using touch count == 2.

To handle pinching you find the distance between the 2 touches looping though all touches each frame to get the touch positions:

And then zoom in or out if the distance changes significantly.

For touch panning you just check that the distance between the two touches doesn't change significantly and then you take the average movement of the two touches as a panning motion.

This video and code from blurst considers handling multi touch reliably:

here is a script i used for zooming using pinch, the Camera game object is assigned in the inspector after its attached to the camera object.

using UnityEngine;
using System.Collections;

public class CameraZoomPinch : MonoBehaviour 
	public int speed = 4;
	public Camera selectedCamera;
	public float MINSCALE = 2.0F;
	public float MAXSCALE = 5.0F;
	public float varianceInDistances = 5.0F;
	private float touchDelta = 0.0F;
	private Vector2 prevDist = new Vector2(0,0);
	private Vector2 curDist = new Vector2(0,0);
	private float startAngleBetweenTouches = 0.0F;
	private int vertOrHorzOrientation = 0; //this tells if the two fingers to each other are oriented horizontally or vertically, 1 for vertical and -1 for horizontal
	private Vector2 midPoint = new Vector2(0,0); //store and use midpoint to check if fingers exceed a limit defined by midpoint for oriantation of fingers
	// Use this for initialization
	void Start () 
	// Update is called once per frame
	void Update () 
		if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Began && Input.GetTouch(1).phase == TouchPhase.Began) 
		if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved) 
			midPoint = new Vector2(((Input.GetTouch(0).position.x + Input.GetTouch(1).position.x)/2), ((Input.GetTouch(0).position.y - Input.GetTouch(1).position.y)/2)); //store midpoint from first touches
			curDist = Input.GetTouch(0).position - Input.GetTouch(1).position; //current distance between finger touches
			prevDist = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition)); //difference in previous locations using delta positions
			touchDelta = curDist.magnitude - prevDist.magnitude;
			if ((Input.GetTouch(0).position.x - Input.GetTouch(1).position.x) > (Input.GetTouch(0).position.y - Input.GetTouch(1).position.y))
				vertOrHorzOrientation = -1; 
			if ((Input.GetTouch(0).position.x - Input.GetTouch(1).position.x) < (Input.GetTouch(0).position.y - Input.GetTouch(1).position.y))
				vertOrHorzOrientation = 1;
						if ((touchDelta < 0)) //
					selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView + (1 * speed),15,90);
			if ((touchDelta > 0))

				selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView - (1 * speed),15,90);

This code has one flaw, i cannot get it to recognize swiping with two fingers, as it tries to zoom when i do that, whether its swiping vertically, horizontally or diagonally. I am not sure how to modify this to work with swipes :confused:
(I want it to zoom even if they swipe but pinch in or out while swiping)

I used this script and it works nicely, thank you for posting it. I used it in conjunction with another script that rotates an object via an x-axis swipe.

The compiler does say that the midpoint variable is declared but not used. I also cannot figure out the use for the minscale and maxscale variables as I cannot discern their effect when i change them.

The script however does what i would like. I am hoping the minscale and maxscale somehow control the movement range of the camera but i can’t tell how to do that. Controlling the range of the camera via this script would be handy.

thanks again. very helpful.

I found this somewhere in a forum as a means of translating an object as you touch the screen. I modified it to rotate an object. Simply attach this script to a gameobjct you want to spin. There is also a speed variable.


// Moves object according to finger movement on the screen

var speed : float = 0.1;
function Update () {
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)

// Get movement of the finger since last frame
var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;

// Move object across XY plane
transform.Rotate (0,-touchDeltaPosition.x * speed, 0);



The code i used was created from a different snippet i made that detected swioes and rotation (hence the midpoint to detect that) you can remove midpoint variable. I also had max and min scale to determine speed of zoom but it was buggy and would inverse my fov which looks odd. The max and min fov are hardcided, just look (or put in those min and max values) in where it specifies the maximum it can be. Look for the mathf. Clamp declaration to figure out those.

Can you post your rotation code? Do you have any ideas to help detect swipes (and still zoom in and out if they are swiping but pinch as well)