How do you differentiate between pinch to zoom with two fingers and a two finger drag?


I would like to have two different actions for pinch to zoom (zooming) and two finger drag (panning). The Zoom function is already working but I don’t know how to define a two finger drag on screen…

		// If there are two touches on the device...
		if (Input.touchCount == 2)
			// Store both touches.
			Touch touchZero = Input.GetTouch(0);
			Touch touchOne = Input.GetTouch(1);

			// Find the position in the previous frame of each touch.
			Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
			Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

			// Find the magnitude of the vector (the distance) between the touches in each frame.
			float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
			float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

			// Find the difference in the distances between each frame.
			float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

			// If the camera is orthographic...
			if (GetComponent<Camera>().orthographic)
				// ... change the orthographic size based on the change in distance between the touches.
				GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;

				// Make sure the orthographic size never drops below zero.
				GetComponent<Camera>().orthographicSize = Mathf.Max(GetComponent<Camera>().orthographicSize, 0.1f);

				// Otherwise change the field of view based on the change in distance between the touches.
				GetComponent<Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;

				// Clamp the field of view to make sure it's between 0 and 180.
				GetComponent<Camera>().fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 3f, 45f);

Anyone got an idea?


Calculate the distance between two touch points, if the distance between these points is greater than or less than (some value, e.g greater than 30f or less than 30f ) consider this as zooming in and zooming out , and if the distance is not meeting your this criteria consider this as panning.

Calculate the direction of each finger from the current frame to the previous frame. Pinches + zooms will either be moving towards each other or away from each other, while drags will be moving in the same general direction.