- Home /
Question by
samz · May 08, 2012 at 10:11 AM ·
touchzoomtouchphasepinch
Holding zoom out pinch
im trying to zoom out when i pinch inwards and hold the zoom while my fingers are touching the screen. And when released it zooms in back to normal. im having trouble tho, this code doesnt allow me to hold the zoom, and its a lil glitchy, like if i zoom out really quickly at times it holds the zoom even when my finger is released
Thank you for you help
private var minPinchDistance : float = 1;
private var pinchRatio : float = 1;
private var curDistance : float;
private var prevDistance : float;
private var pinchDistanceDelta : float = 0;
var gui : GUIText;
function Update()
{
// if two fingers are touching the screen at the same time ...
if (Input.touchCount == 2)
{
var touch1 = Input.touches[0];
var touch2 = Input.touches[1];
}
// ... if at least one of them moved ...
if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved)
{
// ... check the delta distance between them ...
curDistance = Vector2.Distance(touch1.position, touch2.position);
prevDistance = Vector2.Distance(touch1.position - touch1.deltaPosition, touch2.position - touch2.deltaPosition);
pinchDistanceDelta = curDistance - prevDistance;
// ... if it's greater than a minimum threshold, it's a pinch!
if (pinchDistanceDelta > minPinchDistance)
{
pinchDistanceDelta *= pinchRatio;
// which type of pinch is it?
gui.guiText.text = "zoom out pinch";
camera.fieldOfView -= 1;
}
else if ((pinchDistanceDelta < minPinchDistance) && touch1.phase== TouchPhase.Ended)
{
pinchDistanceDelta *= pinchRatio;
// which type of pinch is it?
gui.guiText.text = "zoom in pinch";
//if( !camera.fieldOfView == 32 )
camera.fieldOfView = 32;
}
else
{
curDistance = pinchDistanceDelta = 0;
gui.guiText.text = "thats not a proper pinch!";
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613071235im_/https://answers.unity.com/themes/thub/images/avi.jpg)