Search Unity

Pinch?

Discussion in 'iOS and tvOS' started by mindengine, Dec 7, 2008.

  1. mindengine

    mindengine

    Joined:
    Sep 8, 2008
    Posts:
    114
    Anybody got the pinch gesture up and running?
     
  2. Jeremy-Alessi

    Jeremy-Alessi

    Joined:
    Oct 18, 2007
    Posts:
    125
    I coded up a simple 2 touch routine that checks a vector 2 and whether it's growing or shrinking. I did not see support for gestures built into Unity. I did the same thing for finger flicks, full taps, or slow drags in Debris.
     
  3. kheng

    kheng

    Joined:
    Oct 22, 2008
    Posts:
    126
    Do you mine sharing the code for pinch Jeremy? I've also been struggling how to do this.
     
  4. Jeremy-Alessi

    Jeremy-Alessi

    Joined:
    Oct 18, 2007
    Posts:
    125
    LOL, I forgot how much brute force I used when doing this. You can clean it up but the basic principle is there.

    Code (csharp):
    1.  
    2.     if ( iPhoneInput.touchCount == 2 )
    3.     {
    4.         var touch1 : iPhoneTouch = iPhoneInput.GetTouch( 0 );
    5.         var touch2 : iPhoneTouch = iPhoneInput.GetTouch( 1 );
    6.        
    7.         if ( touch1.position.x < touch2.position.x )
    8.             Camera.main.transform.position.z -= ( touch1.positionDelta.x - touch2.positionDelta.x ) / 10;
    9.         if ( touch1.position.x > touch2.position.x )
    10.             Camera.main.transform.position.z += ( touch1.positionDelta.x - touch2.positionDelta.x ) / 10;
    11.        
    12.         if ( touch1.position.y < touch2.position.y )
    13.             Camera.main.transform.position.z -= ( touch1.positionDelta.y - touch2.positionDelta.y ) / 10;
    14.         if ( touch1.position.y > touch2.position.y )
    15.             Camera.main.transform.position.z += ( touch1.positionDelta.y - touch2.positionDelta.y ) / 10;
    16.  
    17.        
    18.         if ( Camera.main.transform.position.z > -2 )
    19.             Camera.main.transform.position.z = -2;
    20.         if ( Camera.main.transform.position.z < -15 )
    21.             Camera.main.transform.position.z = -15;
    22.  
    23.     }
    24.  
     
  5. kheng

    kheng

    Joined:
    Oct 22, 2008
    Posts:
    126
    Thx Jeremy!
     
  6. blt3d

    blt3d

    Joined:
    Oct 23, 2008
    Posts:
    192
    Yea man nice job! I've been wondering about this too.
     
  7. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    I must fundamentally not grok the meaning of GetTouch and the values it returns.

    I thought GetTouch(0) would get you the first finger, and GetTouch(1) would get you the second finger touching the screen. Which would mean that comparing the x and y's of the two would only tell you how far apart your fingers were, but not which way they were moving. Obviously I'm wrong though since this code obviously works fine in Debris.

    The docs don't seem to help me as what scant description there is of GetTouch, it seems to reinforce my (wrong) assumption. Can someone who understands please explain this?
     
  8. Jeremy-Alessi

    Jeremy-Alessi

    Joined:
    Oct 18, 2007
    Posts:
    125
    If you'll notice there are references to position and to delta. Depending on the fingers' relative positions the delta will tell you if they are diverging or converging.
     
  9. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Ahh, somehow I totally missed that in your code. It all makes sense now. That's what I get for not getting any sleep. Thanks!
     
  10. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    First off, thanks for getting me started on this, Jeremy!

    Okay, I realized that taking action on each axis separately resulted in a situation where if your "pinch" took place along a single axis, the camera would move far less than if your "pinch" occurred diagonally (i.e. taking place along both axes) since your camera got moved once for X and again for Y.

    To solve this, I used the following approach:

    Code (csharp):
    1.  
    2. iPhoneTouch touch = iPhoneInput.GetTouch(0);
    3. iPhoneTouch touch2 = iPhoneInput.GetTouch(1);
    4.  
    5. // Find out how the touches have moved relative to eachother:
    6. Vector2 curDist = touch.position - touch2.position;
    7. Vector2 prevDist = (touch.position - touch.positionDelta) - (touch2.position - touch2.positionDelta);
    8.  
    9. float delta = curDist.magnitude - prevDist.magnitude;
    10.  
    Then "delta" is what you use to move your camera (either along Z in the case of a perspective projection, or to change the orthographicSize in the case of an orthographic projection).

    I hope someone finds that useful.
     
  11. Jeremy-Alessi

    Jeremy-Alessi

    Joined:
    Oct 18, 2007
    Posts:
    125
    That looks more like what I originally thought I wrote LOL. That's sweet!
     
  12. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    What would be the most reliable way to check to see if the player was zooming in or zooming out ?

    thanks
     
  13. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Check whether delta in my example above is positive or negative.
     
  14. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    thanks Brady - did that earlier but the value changes rapidly (set a bool) from true to false even if you are only zooming out
     
  15. SteveB

    SteveB

    Joined:
    Jan 17, 2009
    Posts:
    1,451
    *Digs Up Grave*

    ...was wondering, within this context if anyone attempted to figure out how to do 'rotations' with two fingers. Essentially the pinch and turn you can do for example in Google Earth (pinch zooms, turning fingers rotates world).

    I know this was asked in other threads (that I can't seem to find) so I figured being that Pinch was "solved", perhaps there was some movement on rotates. :)
     
  16. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Hi, all,

    Does anyone have a solution for a swipe-to-move camera script?

    Thanks,

    Greg
     
  17. neoRiley

    neoRiley

    Joined:
    Dec 12, 2008
    Posts:
    162
    To bring it around full circle, here's what Brady meant in total (since there were a few things not mentioned on where/how to use delta:

    Code (csharp):
    1.  
    2. if ( iPhoneInput.touchCount == 2 )
    3. {
    4.     iPhoneTouch touch1 = iPhoneInput.GetTouch( 0 );
    5.     iPhoneTouch touch2 = iPhoneInput.GetTouch( 1 );
    6.    
    7.     // Find out how the touches have moved relative to eachother:
    8.     Vector2 curDist = touch1.position - touch2.position;
    9.     Vector2 prevDist = (touch1.position - touch1.positionDelta) - (touch2.position - touch2.positionDelta);
    10.    
    11.     float touchDelta = curDist.magnitude - prevDist.magnitude;
    12.  
    13.         // move camera in world coordinates
    14.     //Camera.main.transform.position -= new Vector3(0, 0, touchDelta * .5f);
    15.  
    16.         // translate along local coordinate space
    17.     Camera.main.transform.Translate(0,0,touchDelta*.5f);   
    18. }
    19.  
    The code above will give you what you're looking for ;)
     
  18. erina51pratt

    erina51pratt

    Joined:
    Feb 18, 2011
    Posts:
    1
    You can also do this as:

    protected boolean touchEvent(TouchEvent message)
    {
    switch(message.getEvent())
    {
    case TouchEvent.GESTURE:
    TouchGesture gesture = message.getGesture();
    switch(gesture.getEvent())
    {
    case TouchGesture.PINCH_END:
    Dialog.alert("Focal point: " + message.getX(1)
    + ", " + message.getY(1)
    + "\nFinal zoom value: " + gesture.getPinchMagnitude());
    return true;
    }
    }
    return false;
    }
     
  19. kfc

    kfc

    Joined:
    Dec 17, 2010
    Posts:
    27
    Hi guys,

    I'm trying to use the code posted as above.


    function Update ()
    {
    if ( Input.touchCount == 2 )
    {
    var touch1 : Touch = Input.GetTouch( 0 );
    var touch2 : Touch = Input.GetTouch( 1 );

    Vector2 curDist = touch1.position-touch2.position;
    Vector2 prevDist = (touch1.position-touch1.positionDelta)-(touch2.position-touch2.positionDelta);

    float touchDelta = curDist.magnitude-prevDist.magnitude;

    Camera.main.transform.translate(0,0,touchDelta*.5f);

    }
    }


    However, it returns a few error when I was trying to compile it.
    Assets/Standard Assets (Mobile)/Scripts/pinchZoom.js(8,24): UCE0001: ';' expected. Insert a semicolon at the end.
    Assets/Standard Assets (Mobile)/Scripts/pinchZoom.js(9,24): UCE0001: ';' expected. Insert a semicolon at the end.
    Assets/Standard Assets (Mobile)/Scripts/pinchZoom.js(11,22): UCE0001: ';' expected. Insert a semicolon at the end.

    I notice that the semicolon is already in the script.
    can anyone show me how to get it work?
    thanks.
     
  20. SrBilyon

    SrBilyon

    Joined:
    Jul 13, 2010
    Posts:
    291
    *Digs up from grave*

    I believe you are getting that error because you added to a .js and the above was C# This solves KFC problem: