Problem with two finger zoom

Hi,

I’ve written the below code to zoom my mini map in/out but my problem is if I pinch in or out quickly then stop the Size keeps increasing, what I’m trying to do is zoom in/out as I pinch in/out no matter if slowly or quickly I’m trying to match the Size to the dividedZoomDistance so it don’t carry on increasing or decreasing when I stop pinching …

Thank for any help.

		if(Input.touchCount >= 2){
		
			touch0 = Input.GetTouch(0).position;
			touch1 = Input.GetTouch(1).position;
// Only scale if any of the fingers are moving			
			if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved){ 		
				if(!touched){
					touched = true;
					touchedDistance = Vector2.Distance(touch0, touch1);
				}
				touchingDistance = Vector2.Distance(touch0, touch1);
				zoomDistance = touchingDistance - touchedDistance;
				dividedZoomDistance = zoomDistance/600;
				
				Scale = Scale - dividedZoomDistance;
// Round to 1 decimal point
				Scale = Mathf.Round(Scale*10)/10;
				
				if(Scale > 3){
					Scale = 3;
					touched = false;
					touchedDistance = 0;
					touchingDistance = 0;
					zoomDistance = 0;
				}
				if(Scale < 0.1){
					Scale =0.1;
					touched = false;
					touchedDistance = 0;
					touchingDistance = 0;
					zoomDistance = 0;
				}
		
			}
		}else{
			touched = false;
			touchedDistance = 0;
			touchingDistance = 0;
			zoomDistance = 0;
		}

I think you should add this line after calculating zoomDistance:

touchingDistance = Vector2.Distance(touch0, touch1);
zoomDistance = touchingDistance - touchedDistance;
touchedDistance = touchingDistance; //Add this line
dividedZoomDistance = zoomDistance/600;

It is very similar tothings I have done before but I usually abstract it in an static class for gesture recongition so I can use it anywhere. Something like this:

public class GestureRecognizer : MonoBehaviour {
    public static bool zooming{ get; private set; }
    public static float deltaZoom;
    private Vector2 lastRelativePosition = new Vector2(0,0);

void Update () {
    if (Input.touchCount == 2 ){
	if (!zooming) {
		zooming = true;
		lastRelativePosition = Input.GetTouch (1).position -Input.GetTouch (0).position;
		Debug.Log("Zooming");
	} 
	else {
		Vector2 relativePosition = Input.GetTouch (1).position - Input.GetTouch (0).position;
		deltaZoom = relativePosition.magnitude - lastRelativePosition.magnitude;
		lastRelativePosition = Input.GetTouch (1).position - Input.GetTouch (0).position;
    }
    else{
    	zooming = false;	
    }
}

Then you just read deltaZoom in any class you want.


Edit: further explanation.

Let me exaplin better abour that line:

You only time you assign touchedDistance is when you begin to touch the screen, the next frame you move your fingers to a relatively close position. Let´s say that touchedDistance is (1,1) and touching distance is (1,2). The key here is that, as touchedDistance is not beeing changed, you get a positive zoomDistance even if your fingers didn´t move after you reached the second position. That is undesirable, you shouldn´t get any zoom in that case. That´s way in my script I don´t calculate directly distances, but deltas instead. By delta I mean difference. In the images you will see it. If I woudn´t recalculate the old relative position, there would always be a zoom distance, every frame even if my fingers didn´t move!

alt text

alt text

As an example, I use my gesture recognition scripts like this:

//In other script (not the gesgure recognition one
void OnUpdate(){
    if (GestureRecognizer.zooming){
        Console.Debug(deltaZoom);
    }        
}