Create actions based on pinch. TouchDelta? Camera Rotate?

I am curious how to make a pinch functionality in my game. I assume i have to compare two positions of each finger to see if it is meant to be a pinch in or out. How would i do this?

What can i all do with Input.GetTouch(0).deltaPosition? (0 and 1 respectively for each finger)

Would GetTouch(0).position work how i want (current position as a vector2), but delta gives how much it moved last update call (as a vector2)?
How would i get the previous position of each touch to compare if each finger moved towards or away form each other?

The code i have so far is this:

using UnityEngine;
using System.Collections;

public class CameraZoomPinch : MonoBehaviour 
{
	public float speed = 0.1F;
	public Camera selectedCamera;
	private Vector2 touch0pos = new Vector2 (0,0);
	private Vector2 touch1pos = new Vector2 (0,0);
	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (Input.touchCount == 2)
		{
			touch0pos = Input.GetTouch(0).position;
			touch1pos = Input.GetTouch(1).position;	
		}
		
		if (Input.touchCount == 2  Input.GetTouch(0).phase == TouchPhase.Moved  Input.GetTouch(1).phase == TouchPhase.Moved) 
		{
			Vector2 touchDeltaPosition0 = Input.GetTouch(0).deltaPosition;
			Vector2 touchDeltaPosition1 = Input.GetTouch(1).deltaPosition;
			//if ((touchDeltaPosition0.magnitude.CompareTo  touchDeltaPosition1.magnitude ) < 0.0F)
			//{
			//selectedCamera.fieldOfView = selectedCamera.fieldOfView = selectedCamera.fieldOfView + 1;
			//}			
			
			if ((Vector2.Distance(touch0pos,touch1pos)-(Vector2.Distance(touchDeltaPosition0,touchDeltaPosition1)) < 0.0F))
			{
			//selectedCamera.transform.Translate(0, 0, (touchDeltaPosition0.magnitude - touchDeltaPosition1.magnitude) * speed);
			selectedCamera.fieldOfView = selectedCamera.fieldOfView + 1;
				      
			}		
		}
		
	}
}

Right now my code just makes my camera fov increase only positively. Are the delta magnitude values always positive? How is direction represented?

I chose an inspector helped variable for my camera as i believe that is faster than selecting the object each time iteratively.

I also want to figure out rotation of two fingers to rotate. Would i have to track each finger, and figure out the angle they move each second (delta angle with some trig function) and then apply that to my camera’s local euler angle?

How would i do these tasks? Can somebody provide code examples. I learn best form looking at code, but i cannot find examples of delta and making pinches, even though it seems to be a common question. I was also disappointed the unity documentation wasn’t as thorough as I’d like it to be on using delta other than just deltaTime and with examples.

I have tried searching these forums, but no luck on finding HOW to do it in c# (if i saw javascript i could see the idea how to do it) but nobody has gotten code to be given yet people keep asking… :face_with_spiral_eyes:

Hi,

I found this code but zoom dont work on Android. Any help would be appreciated.

///////////////////////////////////////////////////////////
//
//   Author  : Alexander Orozco
//   Email   : alex@rozgo.com
//   License : Keep this notice around. Otherwise, enjoy!
//
///////////////////////////////////////////////////////////

using UnityEngine;
using System.Collections;

public class Gestures : MonoBehaviour {
	
	// adjust accordingly in the inspector
	public float zoomNearLimit = 5;
	public float zoomFarLimit = 12;
	public float zoomScreenToWorldRatio = 3.0f;
	public float orbitScreenToWorldRatio = 1.0f;
	public float twistScreenToWorldRatio = 5.0f;
	
	// don't change these
	Vector3 orbitSpeed = Vector3.zero;
	float twistSpeed = 0;
	float distWeight;
	float zoomDistance;
	float zoomSpeed = 0;
	float lastf0f1Dist;
	
	void Update () {

		// one finger gestures
		if (Input.touchCount == 1) {
			
			// finger data
			Touch f0 = Input.GetTouch(0);
			
			// finger delta
			Vector3 f0Delta = new Vector3(f0.deltaPosition.x, -f0.deltaPosition.y, 0);
			
			// if finger moving
			if (f0.phase == TouchPhase.Moved) {
				
				// compute orbit speed
				orbitSpeed += (f0Delta + f0Delta * distWeight) * orbitScreenToWorldRatio * Time.deltaTime;
			}
		}
		
		// two fingers gestures
		else if (Input.touchCount == 2) {
			
			// fingers data
			Touch f0 = Input.GetTouch(0);
			Touch f1 = Input.GetTouch(1);
			
			// fingers positions
			Vector3 f0Pos = new Vector3(f0.position.x, f0.position.y, 0);
			Vector3 f1Pos = new Vector3(f1.position.x, f1.position.y, 0);
			
			// fingers movements
			Vector3 f0Delta = new Vector3(f0.deltaPosition.x, f0.deltaPosition.y, 0);
			Vector3 f1Delta = new Vector3(f1.deltaPosition.x, f1.deltaPosition.y, 0);
			
			// fingers distance
			float f0f1Dist = Vector3.Distance(f0.position, f1.position);
			
			// if both fingers moving
			if (f0.phase == TouchPhase.Moved  f1.phase == TouchPhase.Moved) {
				
				// fingers moving direction
				Vector3 f0Dir = f0Delta.normalized;
				Vector3 f1Dir = f1Delta.normalized;
				
				// dot product of directions
				float dot = Vector3.Dot(f0Dir, f1Dir);
				
				// if fingers moving in opposite directions
				if (dot < -0.2f) {
					
					float pinchDelta = f0f1Dist - lastf0f1Dist;
					
					// if fingers move more than a threshold
					if (Mathf.Abs(pinchDelta) > 2) {
						
						// if pinch out, zoom in 
						if (f0f1Dist > lastf0f1Dist  zoomDistance > zoomNearLimit) {
							zoomSpeed += (pinchDelta + pinchDelta * distWeight) * Time.deltaTime * zoomScreenToWorldRatio;
						}
						
						// if pinch in, zoom out
						else if (f0f1Dist < lastf0f1Dist  zoomDistance < zoomFarLimit) {
							zoomSpeed += (pinchDelta + pinchDelta * distWeight) * Time.deltaTime * zoomScreenToWorldRatio;
						}
					}
					
					// detect twist
					if (f0Delta.magnitude > 2  f1Delta.magnitude > 2) {
						
						// homemade algorithm works, but needs code review
						Vector3 fingersDir = (f1Pos - f0Pos).normalized;
						Vector3 twistNormal = Vector3.Cross(fingersDir, Vector3.forward);
						Vector3 twistAxis = Vector3.Cross(fingersDir, twistNormal);
						float averageDelta = (f0Delta.magnitude + f1Delta.magnitude) / 2;
						if (Vector3.Dot(f0Dir, twistNormal) > 0.2f) {
							twistSpeed =  twistAxis.z * averageDelta * Time.deltaTime * twistScreenToWorldRatio;
						}
						else if (Vector3.Dot(f0Dir, twistNormal) < -0.2f) {
							twistSpeed = -twistAxis.z * averageDelta * Time.deltaTime * twistScreenToWorldRatio;
						}
					}
				}
			}
			
			// record last distance, for delta distances
			lastf0f1Dist = f0f1Dist;
			
			// decelerate zoom speed
			zoomSpeed = zoomSpeed * (1 - Time.deltaTime * 10);
		}
		
		// no touching, or too many touches (we don't care about)
		else {
			
			// bounce to zoom limits
			if (zoomDistance < zoomNearLimit) {
				zoomSpeed += (zoomDistance - zoomNearLimit) * zoomScreenToWorldRatio;
			}
			else if (zoomDistance > zoomFarLimit) {
				zoomSpeed += (zoomDistance - zoomFarLimit) * zoomScreenToWorldRatio;
			}
			
			// or decelerate
			else {
				zoomSpeed = zoomSpeed * (1 - Time.deltaTime * 10);
			}
		}
		
		// decelerate orbit speed
		orbitSpeed = orbitSpeed * (1 - Time.deltaTime * 5);

		// decelerate twist speed
		twistSpeed = twistSpeed * (1 - Time.deltaTime * 5);

		// apply zoom
		transform.position += transform.forward * zoomSpeed * Time.deltaTime;
		zoomDistance = transform.position.magnitude;
		
		// apply orbit and twist
		transform.position = Vector3.zero;
		transform.localRotation *= Quaternion.Euler(orbitSpeed.y, orbitSpeed.x, twistSpeed);
		transform.position = -transform.forward * zoomDistance;
		
		// compensate for distance (ej. orbit slower when zoomed in; faster when out)
		distWeight = (zoomDistance - zoomNearLimit) / (zoomFarLimit - zoomNearLimit);
		distWeight = Mathf.Clamp01(distWeight);
	}
}

Max

Might be a little too late, but I found this on google, so other people might search for it too.

Unity’s wiki has a prewritten script for pinch (zoom) and rotate (…rotate) right here. Works perfectly on my android device. Good Luck !