Search Unity

How to detect just tap and swipe left and right ?

Discussion in 'Scripting' started by vistriter, Oct 7, 2017.

  1. vistriter

    vistriter

    Joined:
    Jan 22, 2017
    Posts:
    91
    I want to detect tap and swipe left and right. But I have problem about tap. When swipe it's must tap it's error.

    this is code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MobileController : MonoBehaviour {
    6.  
    7.     private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    8.     private bool isDraging = false;
    9.     private Vector2 startTouch, swipeDelta;
    10.  
    11.     private void Update()
    12.     {
    13.         tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
    14.  
    15.         #region Standalone Inputs
    16.  
    17.             if(Input.GetMouseButtonDown(0))
    18.             {
    19.                 tap = true;
    20.                 isDraging = true;
    21.                 startTouch = Input.mousePosition;
    22.             }
    23.             else if(Input.GetMouseButtonUp(0))
    24.             {
    25.                 isDraging = false;
    26.                 Reset();
    27.             }
    28.  
    29.         #endregion
    30.  
    31.         #region Mobile Input
    32.  
    33.             if(Input.touches.Length > 0)
    34.             {
    35.                 if(Input.touches[0].phase == TouchPhase.Began)
    36.                 {
    37.                     tap = true;
    38.                     isDraging = true;
    39.                     startTouch = Input.touches[0].position;
    40.                 }
    41.                 else if(Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
    42.                 {
    43.                     isDraging = false;
    44.                     Reset();
    45.                 }
    46.             }
    47.  
    48.         #endregion
    49.  
    50.         // Calculate the distance
    51.  
    52.         swipeDelta = Vector2.zero;
    53.  
    54.         if(isDraging)
    55.         {
    56.             if(Input.touches.Length > 0)
    57.             {
    58.                 swipeDelta = Input.touches[0].position - startTouch;
    59.             }
    60.             else if(Input.GetMouseButton(0))
    61.             {
    62.                 swipeDelta = (Vector2) Input.mousePosition - startTouch;
    63.             }
    64.         }
    65.  
    66.         // Did we cross the deadzone ?
    67.         if(swipeDelta.magnitude > 100f)
    68.         {
    69.             // Which direction ?
    70.             float x = swipeDelta.x;
    71.             float y = swipeDelta.y;
    72.  
    73.             if(Mathf.Abs(x) > Mathf.Abs(y))
    74.             {
    75.                 // Left or right
    76.                 if(x < 0)
    77.                 {
    78.                     swipeLeft = true;
    79.                 }
    80.                 else
    81.                 {
    82.                     swipeRight = true;
    83.                 }
    84.             }
    85.             else
    86.             {
    87.                 // Up or down
    88.                 if(y < 0)
    89.                 {
    90.                     swipeDown = true;
    91.                 }
    92.                 else
    93.                 {
    94.                     swipeUp = true;
    95.                 }
    96.             }
    97.  
    98.             Reset();
    99.  
    100.         }
    101.  
    102.     }
    103.  
    104.     private void Reset()
    105.     {
    106.         startTouch = swipeDelta = Vector2.zero;
    107.         isDraging = false;
    108.     }
    109.  
    110.     public Vector2 SwipeDelta { get { return swipeDelta; } }
    111.     public bool SwipeLeft { get { return swipeLeft; } }
    112.     public bool SwipeRight { get { return swipeRight; } }
    113.     public bool SwipeUp { get { return swipeUp; } }
    114.     public bool SwipeDown { get { return swipeDown; } }
    115.     public bool Tap { get { return tap; } }
    116.  
    117.  
    118.  
    119. }
    120.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Your code blindly assumes that as soon as their finger touches the screen that it's a tap. You'll want to wait a little bit and see if the finger moves over the next few frames before assuming it's a tap.