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 …
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:
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!
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);
}
}
That should be easy (although I would do it in two different scripts. Conceptually, input and scaling are different to me). Something like this should work: //In other script private float Scale; float someConstantForZoomingVelocity; void Update(){ if (GestureRecogition.zooming){ Scale += GestureRecognition.deltaZoom; //You may want a constant float to get an adequate velocity //Check max and minimum values } }
That should be easy (although I would do it in two different scripts. Conceptually, input and scaling are different to me). Something like this should work: //In other script private float Scale; float someConstantForZoomingVelocity; void Update(){ if (GestureRecogition.zooming){ Scale += GestureRecognition.deltaZoom; //You may want a constant float to get an adequate velocity //Check max and minimum values } }
– MrVerdoux