- Home /
How do you differentiate between pinch to zoom with two fingers and a two finger drag?
Hi,
I would like to have two different actions for pinch to zoom (zooming) and two finger drag (panning). The Zoom function is already working but I don't know how to define a two finger drag on screen...
// If there are two touches on the device...
if (Input.touchCount == 2)
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
// If the camera is orthographic...
if (GetComponent<Camera>().orthographic)
{
// ... change the orthographic size based on the change in distance between the touches.
GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
// Make sure the orthographic size never drops below zero.
GetComponent<Camera>().orthographicSize = Mathf.Max(GetComponent<Camera>().orthographicSize, 0.1f);
}
else
{
// Otherwise change the field of view based on the change in distance between the touches.
GetComponent<Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
// Clamp the field of view to make sure it's between 0 and 180.
GetComponent<Camera>().fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 3f, 45f);
}
}
Anyone got an idea?
thanks
exclusive. when there is a zoom there shouldn't be a drag. :)
You can compare the magnitude over frame. If the difference is below a threshold, then it is a drag, else it is a zoom.
You could also check over several frames what is the tendency, is the magnitude changing or always within a range. Based on that, you lock the corresponding action until fingers are removed from screen. This way you don't end up switching action as you move.
Hmmm. Pinches go towards each other or away. Drags go the same way, relatively.
Answer by UnitedCoders · Feb 08, 2018 at 08:01 AM
Calculate the distance between two touch points, if the distance between these points is greater than or less than (some value, e.g greater than 30f or less than 30f ) consider this as zooming in and zooming out , and if the distance is not meeting your this criteria consider this as panning.
Answer by Deathdefy · Feb 07, 2018 at 09:07 PM
Calculate the direction of each finger from the current frame to the previous frame. Pinches + zooms will either be moving towards each other or away from each other, while drags will be moving in the same general direction.