Panning & Zooming using Multi Touch

Lately I’ve been trying to make my camera zoom in when pinching and pan when swiping. But I have run into some problems. I know that you need to start with:

if(Input.touchCount == 2) {
//the rest
}

But how can I do the rest? I know you need to calculate distances between your two fingers. But how can you do the rest? Like how can you make sure that pinches don’t get confused with swipes.

If anyone has any scripts that I can use, or some way to enlighten me (pseudo code or tuts), that would be great. Thanks!

An easy solution would be to only allow panning with a single touch and switch to zooming when there are two touches.

if(Input.touchCount == 1)
{
    PanUpdate();
}
else if(Input.touchCount == 2)
{
    ZoomUpdate();
}

If you don’t want to go that route, you may have to apply some heuristics to deciding whether the user intends a zoom or a pan. One that might work is to allow a pan when there is one touch. If there are two touches, try to figure out their movement vectors using either touch.position or touch.deltaPosition. If the vectors are pointing in generally the same direction, then it is a pan gesture. If they appear to be opposites or nearly opposites, it is likely a pinch/zoom gesture.

if(Input.touchCount == 1)
{
    PanUpdate();
}
else if(Input.touchCount == 2)
{
    Vector3 touch1Dir = Input.touches[0].deltaPosition.normalized;
    Vector3 touch2Dir = Input.touches[1].deltaPosition.normalized;

    float dotProduct = Vector2.Dot(touch1Dir, touch2Dir);

    if(dotProduct is near -1) //pinch/zoom
    {
        ZoomUpdate();
    }
    else if(dotProduct is near 1) //pan gesture
    {
        PanUpdate();
    }
}

I haven’t actually tested this code, so there may be issues with it, but this would be my initial approach to solving this problem. The part that requires playtesting is how near to -1 or 1 the dot product needs to be for the functionality to work correctly.