- Home /
 
Swipe Control Issue.. Need help
I'm having some troubles with creating swipe controls for a game. I'm trying to recreate the controls of the game Subway Surfers, where a swipe is detected during the Touch.Moved phase not the Touch.End phase. Right now it works most of the time but once in a while (to often) a vertical swipe will be processed as a horizontal one. I've posed the code below, thanks in advance for any help.
        int swipeIndex = -1; //index of first detected swipe to prevent multiple swipes
        Vector2 startPos = Vector.zero; //starting position of touch
        //minSwipeDist is the distance in inches the player must swipe for a correct swipe
        for (int i = 0; i < Input.touchCount; i++) {
             Touch touch = Input.touches[i];
             switch (touch.phase) {
                 case TouchPhase.Began:
                     if (swipeIndex == -1) {
                         swipeIndex = i;
                         startPos = touch.position;
                     }
                     break;
                 case TouchPhase.Moved:
                     if(i != swipeIndex)
                         break;
                     Vector2 direction = touch.position - startPos;
                     //vertical swipe
                     if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y)) {
                         //swipe up
                         if ((touch.position.y - startPos.y) > minSwipeDist) {
                             //Process Up swipe
                             swipeIndex = -1;
                         }
                         //swipe down
                         else if ((touch.position.y - startPos.y) < -minSwipeDist) {
                             //Process down swipe
                             swipeIndex = -1;
                         }
                     }
                     //horizontal swipe
                     else {
                         //swipe left
                         if ((touch.position.x - startPos.x) < -minSwipeDist) {
                             //process left swipe
                             swipeIndex = -1;
                         }
                         //swipe right
                         else if ((touch.position.x - startPos.x) > minSwipeDist) {
                             //process right swipe
                             swipeIndex = -1;
                         }
                     }
                     break;
             }
         }
 
              Answer by Graham-Dunnett · Mar 04, 2014 at 10:36 AM
When you first start moving it's easy for the direction code to get it you. You check (line 18) that the touch has moved less in x than in y, and that means a vertical swipe. But, if x==1 and y==2, that does not mean that the swipe will be vertical, it's basically too small a change to know. You probably want to wait until the finger has moved 5% of the screen width or height before deciding what direction they are moving in. You kind of do this with you minSwipeDist variable, but I think you'd want to do this test before you decide there is a swipe. 
If I understood correctly, this is what you mean ?? Even though the same issue occurs. The very first touch gets some random behavior.
 foreach(Touch touch in Input.touches)
                     {
                         Vector2 touchPosition = touch.position;
                         
                         if(touch.phase == TouchPhase.Began && swipeID == -1)
                         {
                             swipeID = touch.fingerId;
                             startPos = touchPosition;
                         } 
                         else if(touch.fingerId == swipeID)
                         {
                             Vector2 delta = touchPosition - startPos;
                             
                             if(touch.phase == TouchPhase.$$anonymous$$oved && delta.magnitude > $$anonymous$$$$anonymous$$ovement)
                             {
                                 swipeID = -1;
                                 if($$anonymous$$athf.Abs(delta.x) > $$anonymous$$athf.Abs (delta.y))
                                 {
                                     if(delta.x > 0)
                                         swipeRight = true;
                                     else
                                         swipeLeft = true;
                                 }
                                 else{
                                     if(delta.y > 0)
                                         swipeUp = true;
                                     else
                                         swipeDown = true;
                                 }
                             }
                         }
                     }
                 Your answer