Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Finger Gesture Script

Discussion in 'Scripting' started by Nigey, Jun 23, 2014.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi guys,

    I'm wondering how you'd go about creating a method to recognize advanced finger gestures?

    I have a script that recognizes when you're swiping up, down, left and right. Like so:

    I'm wondering how you go about making code for recognizing drawing a circle or a square with your finger. Even an arching swipe as opposed to a simple right and left swipe.

    Any ideas on the actual method behind it?
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Forgot to add code lol..

    Code (CSharp):
    1.         // Mobile touch interface
    2.         int fingerCount = 0;
    3.        
    4.         foreach (Touch touch in Input.touches)
    5.         {
    6.             if(touch.phase == TouchPhase.Began)
    7.             {
    8.                 initTouchPos = touch.position;
    9.             }
    10.            
    11.             if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
    12.             {
    13.                 fingerCount++;
    14.                
    15.                 if(fingerCount == 1 && touch.phase == TouchPhase.Began)
    16.                 {
    17.                     Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    18.                     RaycastHit hit;
    19.                    
    20.                     if (Physics.Raycast(ray, out hit))
    21.                     {
    22.                         if(hit.transform.tag == "ButtonPush" || hit.transform.tag == "ButtonPadAbove" || hit.transform.tag == "ButtonPadBelow" || hit.transform.tag == "ButtonShield" || hit.transform.tag == "ButtonRocket")
    23.                         {
    24.                             hit.collider.GetComponent<ButtonScript>().AttemptPress();
    25.                         }
    26.                     }
    27.                 }
    28.  
    29.                 if(fingerCount == 2 && touch.phase == TouchPhase.Began)
    30.                 {
    31.                     oneTouch = Input.GetTouch(0).position;
    32.                     twoTouch = Input.GetTouch(1).position;
    33.  
    34.                     fingerDistance = Vector2.Distance(oneTouch, twoTouch);
    35.                 }
    36.                 if(fingerCount == 2 && touch.phase != TouchPhase.Began)
    37.                 {
    38.                     float newfingerDist = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);
    39.  
    40.                     if(fingerDistance != newfingerDist)
    41.                     {
    42.                         Camera.main.transform.localPosition += new Vector3(0,0, (newfingerDist - fingerDistance) );
    43.                     }
    44.                 }
    45.                    
    46.                 if(fingerCount == 1 && touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Ended)
    47.                 {
    48.                     if(initTouchPos.y < touch.position.y)
    49.                     {
    50.                         move.MoveIt(transform.up);
    51.                     }
    52.                     if(initTouchPos.y > touch.position.y)
    53.                     {
    54.                         move.MoveIt(-transform.up);
    55.                     }
    56.                 }
    57.                
    58.                 /*if(fingerCount == 1 && touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Ended)
    59.                 {
    60.                     Vector2 touchFacing = (initTouchPos - touch.position).normalized;
    61.                    
    62.                     if(Vector2.Dot(touchFacing, Vector2.up) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    63.                     {
    64.                         Ability("SwipeDown");
    65.                     }
    66.                     if(Vector2.Dot(touchFacing, -Vector2.up) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    67.                     {
    68.                         Ability("SwipeUp");
    69.                     }
    70.                     if(Vector2.Dot(touchFacing, Vector2.right) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    71.                     {
    72.                         Ability("SwipeLeft");
    73.                     }
    74.                     if(Vector2.Dot(touchFacing, -Vector2.right) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    75.                     {
    76.                         Ability("SwipeRight");
    77.                     }
    78.                 }*/
    79.             }
    80.         }
    81.     }
     
  3. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702