2 finger scrolling

I couldn’t find it anywhere, so here I ask:

What’s the concept for 2-finger scrolling?
I made my check like this:

function Update()
{
   if (Input.touchCount == 2)
   {
      var dist : float = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);
      var deltaFingerDistance : float;

      if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began)
      {
         lastFingerDistance = dist;
         deltaFingerDistance = 0;
      }
      else
      {
         deltaFingerDistance = dist-lastFingerDistance;
      }
      lastFingerDistance = dist

      if (Mathf.Abs(deltaFingerDistance)>0.5)
      {
         //pinch zoom code here
      }
      else
      {
         if (dist<10)
         {
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
                initFingerPos = Input.GetTouch(0).position;
                cameraInitPos = theCamera.transform.position;
            }
            theCamera.transform.position = cameraInitPos + (Input.GetTouch(0).position-initFingerPos);
         }
      }
   }
}

The panning right now seems jumping over here and there, so I guess there’s something wrong with the camera panning code, but it’s hard to figure out where it went wrong.

Thanks in Advance,

1 Answer

1

For me, that seems like a confusing way to deal with touch input. That being said, I'm a buffoon.

Below is how we do pan/zoom for touchscreen.

The following script started as the camera orbit script that comes in the Standard Assets folder.

We use three fingers for pan and two fingers for zoom, but you get the idea...


var panTranslate  :  Vector3  =  Vector3.zero;
var distance      :  float    =  5.0;

/* vars used to track camera rotation */

var rotation      :  Quaternion;
var position      :  Vector3;
var x             :  float;
var y             :  float;

function LateUpdate ()
{
    /* zoom in/out for two finger pinch */

    if ( Input.touchCount == 2 ) {

        var fingerOne    =  Input.GetTouch(0);
        var fingerTwo    =  Input.GetTouch(1);
        var currentDist  =  fingerOne.position - fingerTwo.position;

        var deltaOne     =  fingerOne.position - fingerOne.deltaPosition;
        var deltaTwo     =  fingerTwo.position - fingerTwo.deltaPosition;
        var lastDist     =  deltaOne - deltaTwo;

        var deltaDist    =  currentDist.magnitude - lastDist.magnitude;
        var theDelta     =  deltaDist * Time.deltaTime;

        distance        -=  theDelta * zoomSpeed * Mathf.Abs(distance); 

        /* clamp the distance so you can't get too close or far */

        distance = clampDistance (distance, distanceMinLimit, distanceMaxLimit);
    }

    /* pan for three touches */

    if ( Input.touchCount == 3 ) {

        var totalMove = Vector2.zero;

        for ( var thisTouch : Touch in Input.touches ) {
            totalMove += thisTouch.deltaPosition;
        }

        /* calculate average movement for each touch */

        var panMove    =  totalMove / Input.touchCount;

        var deltaPanY  =  panMove.y * transform.up    * distance * speedFactor;
        var deltaPanX  =  panMove.x * transform.right * distance * speedFactor;

        panTranslate  -=  deltaPanY;
        panTranslate  -=  deltaPanX;

        /* clamp the pan vector to constrain movement */

        panTranslate = clampPanTranslate(panTranslate);
    }

    /* ignore rotation for this example... */

    rotation = Quaternion.Euler(y, x, 0);
    position = rotation * Vector3(0.0, 0.0, -distance) + target.position + panTranslate;

    transform.rotation = rotation;
    transform.position = position;
}

Hi - i receive a lot of errors on the above jahroy for missing variable declarations... i tried to figure most being floats, but some aren't. Can you post those?