Search Unity

Swipe Gesture

Discussion in 'iOS and tvOS' started by El3et, Jun 28, 2012.

  1. El3et

    El3et

    Joined:
    Jan 19, 2011
    Posts:
    97
    Hi, I need to know how to find out if the user has used the swipe gesture.

    I don't want the swipe gesture to move anything, calculatge velocity i just need the code to know if the user has used the gesture. I use c#.

    Code (csharp):
    1.  
    2.  
    3. public void hasUsedSwipe(){
    4.    
    5.      if(usedSwipe){
    6.      //do something
    7.      }
    8.  
    9. }
    10.  
    Basically thats all i need. I just dont no how to check if the swipe has occurred.

    Thanks
     
  2. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
    look up input.gettouch

    http://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html

    then use the first example

    Code (csharp):
    1. // Moves object according to finger movement on the screen
    2.  
    3. var speed : float = 0.1;
    4. function Update () {
    5.     if (Input.touchCount > 0  
    6.       Input.GetTouch(0).phase == TouchPhase.Moved) {
    7.    
    8.         // Get movement of the finger since last frame
    9.         var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
    10.        
    11.         // Move object across XY plane
    12.         transform.Translate (-touchDeltaPosition.x * speed,
    13.                     -touchDeltaPosition.y * speed, 0);
    14.     }
    15. }
    now where it says

    Code (csharp):
    1.   // Move object across XY plane
    2.         transform.Translate (-touchDeltaPosition.x * speed,
    3.                     -touchDeltaPosition.y * speed, 0);
    change that to say if the touchDeltaPosition.x is greater then whatever distance you want the user to need to move, the user swyped left or right (use Y for up and down)

    Code (csharp):
    1.  
    2.  
    3. if(touchDeltaPosition.x >1)
    4. {
    5.  SwypeRight();
    6. }
    7.  
    8.  
    9.  
     
  3. m4rc0mw4r

    m4rc0mw4r

    Joined:
    Oct 29, 2012
    Posts:
    50
    I've got a question about this.
    Something like if SwypeRight = true, then play animation "..."
     
    DebonairGaming likes this.
  4. laserlars

    laserlars

    Joined:
    Nov 17, 2011
    Posts:
    255
    Read again :eek:)
     
    Last edited: Oct 31, 2012
  5. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    What I do to detect swipes is get the initial point of the touch and save it, then on touch move I compare the initial point with the new point and if the distance between them (sqrmagnitude distance... faster then Vector3.distance) is > then the desired input move.

    On this matter what I would love to know is if there is some way to detect complex touches... for example... detect if the user made a circle move... a square move... etc, but I dont think that is very simple.

    Best regards,
    Allan
     
  6. klattimer

    klattimer

    Joined:
    May 24, 2013
    Posts:
    4
    The cited methods for performing a swipe gesture I've found woefully inadequate for my particular scenario. So I've written a better method which I will provide here;

    How this works is very simple, a little bit of pythag and a little trig to work out the distance the finger has travelled and the angle at which it has travelled. If the speed, distance and angle are within our particular requirements we react. This is easily customisable code but as the other examples above didn't fit I threw this together fairly quickly to produce reasonable results.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SwipeGesture : MonoBehaviour {
    6.     Vector2 startPosition;
    7.     float startTime;
    8.    
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if (Input.touchCount > 0  Input.GetTouch(0).phase == TouchPhase.Ended) {
    17.             Vector2 endPosition = Input.GetTouch(0).position;
    18.             Vector2 delta = endPosition - startPosition;
    19.            
    20.             float dist = Mathf.Sqrt(Mathf.Pow(delta.x, 2) + Mathf.Pow (delta.y, 2));
    21.             float angle = Mathf.Atan (delta.y/delta.x) * (180.0f/Mathf.PI);
    22.             float duration = Time.time - startTime;
    23.             float speed = dist/duration;
    24.            
    25.             // Left to right swipe
    26.             if (startPosition.y < endPosition.y) {
    27.                 if (angle < 0) angle = angle * 1.0f;
    28.                 Debug.Log ("Distance: " + dist + " Angle: " + angle + " Speed: " + speed);
    29.                
    30.                 if (dist > 300  angle < 10  speed > 1000) {
    31. // Do something related to the swipe
    32.                 }
    33.             }
    34.         }
    35.        
    36.         if (Input.touchCount > 0  Input.GetTouch (0).phase == TouchPhase.Began) {
    37.             startPosition = Input.GetTouch(0).position;
    38.             startTime = Time.time;
    39.         }
    40.     }
    41. }
    42.  
    Hope this helps.
     
    Last edited: May 31, 2013
  7. klattimer

    klattimer

    Joined:
    May 24, 2013
    Posts:
    4
    For reference, I attach this script to the main camera
     
  8. jasonMcintosh

    jasonMcintosh

    Joined:
    Jul 4, 2012
    Posts:
    74
    Code (csharp):
    1. if (angle < 0) angle = angle * 1.0f;
    Did you mean...?
    Code (csharp):
    1. if (angle < 0) angle = angle * -1.0f;
     
  9. s1lent-warrior

    s1lent-warrior

    Joined:
    Aug 3, 2014
    Posts:
    5
    Here is a simple approach to detect a swipe in LTR (left to right) and RTL (right to left) directions:

    Code (CSharp):
    1. private Vector3 startPosition = Vector3.zero;
    2. private Vector3 endPosition = Vector3.zero;
    3.  
    4. void Update()
    5. {
    6.     if (Input.GetMouseButtonDown(0))    // swipe begins
    7.     {
    8.         startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    9.     }
    10.     if (Input.GetMouseButtonUp(0))    // swipe ends
    11.     {
    12.         endPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    13.     }
    14.  
    15.     if (startPosition != endPosition && startPosition != Vector3.zero && endPosition != Vector3.zero)
    16.     {
    17.         floatdeltaX = endPosition.x - startPosition.x;
    18.         floatdeltaY = endPosition.y - startPosition.y;
    19.         if ((deltaX > 5.0f || deltaX < -5.0f) && (deltaY >= -1.0f || deltaY <= 1.0f))
    20.         {
    21.             if (startPosition.x < endPosition.x) // swipe LTR
    22.             {
    23.                 print("LTR");
    24.             } else // swipe RTL
    25.             {
    26.                 print("RTL");
    27.             }
    28.         }
    29.         startPosition = endPosition = Vector3.zero;
    30.     }
    31. }
    Hope this helps!
     
    anilkumar.ajc likes this.
  10. anilkumar.ajc

    anilkumar.ajc

    Joined:
    Aug 26, 2014
    Posts:
    1
  11. SamA_Cigna

    SamA_Cigna

    Joined:
    Dec 17, 2018
    Posts:
    1
    Why the hell would you post code that doesn't even compile?
     
  12. Bryarey

    Bryarey

    Joined:
    Aug 24, 2014
    Posts:
    25
    I searched for the same thing, and found it reasonable to create asset for easy swipe detection, and share it with community. So here it is on github. My solution supports different usecases, including: 8-directions swipe detection, 4-directions, 2-directions (left-right or up-down), swipes on hexagonal grid. All listed is included as a presets, but also you can configure it to detect any number of Vector3 directions. So ti`s really flexible. Also, you can try WebGL build or see video tutorial. If you try it, please, let me know (via youtube comment, or see Contacts section on github), was it suitable for your case, and was it comfortable enough.
     
    Argenuto likes this.