I have a problem with my current code, it works to detect the zoom, and i think my code detects the difference between rotation and pinch, but if i put two fingers down and swipe it still zooms. How do i detect this and stop it? I also have to accomodate for users being sloppy when pinching and zoom and move fingers sideways a little bit (probably defining a variable that can be changed)
Here is my current code:
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 minPinchSpeed = 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 speedTouch0 = 0.0F;
private float speedTouch1 = 0.0F;
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)
{
startAngleBetweenTouches = Vector2.Angle(Input.GetTouch(0).position, Input.GetTouch(1).position); //to determine that gesture isnt a rotation
midPoint = new Vector2((Input.GetTouch(0).position.x - Input.GetTouch(1).position.x), (Input.GetTouch(0).position.y - Input.GetTouch(1).position.y)); //store midpoint from first touches
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 (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
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;
speedTouch0 = Input.GetTouch(0).deltaPosition.magnitude / Time.deltaTime;
speedTouch1 = Input.GetTouch(1).deltaPosition.magnitude / Time.deltaTime;
//(curDist < prevDist --- inward pinch) && (if touch0 is fast enough) && (if touch1 is fast enough) && (angle of two touches-using vector2.angle-is less than an angle limit to make sure pinch isnt a rotation)
if ((touchDelta < 1) && (speedTouch0 > minPinchSpeed) && (speedTouch1 > minPinchSpeed) && (Vector2.Angle(Input.GetTouch(0).position, Input.GetTouch(1).position) < (startAngleBetweenTouches+10)) && (Vector2.Angle(Input.GetTouch(0).position, Input.GetTouch(1).position) > (startAngleBetweenTouches-10))) //
{
//(if the two touches are oriented vertically) && (if touch0 x is less than right side of midpoint x) or (if touch0 x is greater than left of midpoint x) && (if touch1 x is less than right side of midpoint x) or (if touch1 x is greater than left of midpoint x) (all values are assuming left and right variances of 20 pixels)
if ((vertOrHorzOrientation == 1) && ((Input.GetTouch(0).position.x < midPoint.x + 20) || (Input.GetTouch(0).position.x > midPoint.x -20)) && ((Input.GetTouch(1).position.x < midPoint.x + 20) || (Input.GetTouch(1).position.x > midPoint.x -20)))
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView + (1 * speed),15,90);
}
//(same as above, but these checks are for horizontal orientation of touches and the y components are checked for limiting the direction)
if ((vertOrHorzOrientation == -1) && ((Input.GetTouch(0).position.y < midPoint.y + 20) || (Input.GetTouch(0).position.y > midPoint.y -20)) && ((Input.GetTouch(1).position.y < midPoint.y + 20) || (Input.GetTouch(1).position.y > midPoint.y -20)))
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView + (1 * speed),15,90);
}
}
if ((touchDelta > 1) && (speedTouch0 > minPinchSpeed) && (speedTouch1 > minPinchSpeed) && (Vector2.Angle(Input.GetTouch(0).position, Input.GetTouch(1).position) < (startAngleBetweenTouches+10)) && (Vector2.Angle(Input.GetTouch(0).position, Input.GetTouch(1).position) > (startAngleBetweenTouches-10)))
{
if ((vertOrHorzOrientation == 1) && ((Input.GetTouch(0).position.x < midPoint.x + 20) || (Input.GetTouch(0).position.x > midPoint.x -20)) && ((Input.GetTouch(1).position.x < midPoint.x + 20) || (Input.GetTouch(1).position.x > midPoint.x -20)))
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView - (1 * speed),15,90);
}
if ((vertOrHorzOrientation == -1) && ((Input.GetTouch(0).position.y < midPoint.y + 20) || (Input.GetTouch(0).position.y > midPoint.y -20)) && ((Input.GetTouch(1).position.y < midPoint.y + 20) || (Input.GetTouch(1).position.y > midPoint.y -20)))
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView - (1 * speed),15,90);
}
}
}
}
}
How do i edit this to detect if a user swipe with two fingers and pinches to zoom?
(i also have to take into account a user can have the pinch horizontal, vertical, or a combo of the two diagonally which i tried doing with the midpoint code.