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

Question Detect Touch Tap and Double Tap

Discussion in 'Scripting' started by Artpen, May 14, 2022.

  1. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    Quick question guys,
    I found a way to get Tap and Double tap. But can't figure out how to prevent single tap when I double taping ?
    Any help much appreciated .

    Thanks

    Code (CSharp):
    1. private void HandleTapInput()
    2.     {
    3.         if (Input.touchCount == 1)
    4.         {
    5.             if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    6.             {
    7.                 // Check touch time
    8.                 if (Input.GetTouch(0).phase == TouchPhase.Began)
    9.                 {
    10.                     touchTime = Time.time;
    11.                 }
    12.  
    13.                 // Check touch end time
    14.                 if (Input.GetTouch(0).phase == TouchPhase.Ended && !isDoubleTap)
    15.                 {
    16.                     touchEndTime = Time.time;
    17.  
    18.                     // if within tap threshold do tap
    19.                     if (touchEndTime - touchTime < tapThreshold)
    20.                     {
    21.                         // Tap
    22.                         Debug.Log("Tap at position:" + Input.GetTouch(0).position);
    23.                         OnTapInputHandler?.Invoke(Input.GetTouch(0).position);
    24.                     }
    25.                 }
    26.             }
    27.         }
    28.     }
    29.  
    30.     // Handle double tap usualy for edit mode, double tap to remove or add
    31.     private void HandleDoubleTap()
    32.     {
    33.         if (Input.touchCount == 1)
    34.         {
    35.             if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    36.             {
    37.                 if(Input.GetTouch(0).tapCount == 2 && Input.GetTouch(0).phase == TouchPhase.Began)
    38.                 {
    39.                     //Handle double tap
    40.                     Debug.Log("Double Tap at position:" + Input.GetTouch(0).position);
    41.                     OnDoubleTapInputHandler?.Invoke(Input.GetTouch(0).position);
    42.                 }
    43.             }
    44.         }
    45.     }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    The only way to check this is to delay the functionality of your single tap until after it's released or the time between double tap expires. You'll probably have to play around with a time that feels comfortable.
     
    Kurt-Dekker likes this.
  3. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    I also thought about something like this. But I think I need to sort my tap count and use it as bool after. I want tap or double tap to be executed in Phase.Ended.
    Code (CSharp):
    1. if(Input.touchCount == 1)
    2.         {
    3.             if(Input.GetTouch(0).phase == TouchPhase.Began)
    4.             {
    5.                 tapCount++;
    6.                 touchTime = Time.time;
    7.             }
    8.  
    9.             if(Input.GetTouch(0).phase == TouchPhase.Ended)
    10.             {
    11.                 if (tapCount == 1)
    12.                 {
    13.                     touchEndTime = Time.time;
    14.  
    15.                     if (touchEndTime - touchTime < tapThreshold)
    16.                     {
    17.                         Debug.Log("Single Tap");
    18.                     }
    19.                 }
    20.  
    21.                 if (tapCount == 2)
    22.                 {
    23.                     Debug.Log("Double Tap");
    24.                 }
    25.             }
    26.        
    27.         }
     
  4. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    The problem is I need to get time elapsed from first to second tap. If time between first and second is grater then double tap threshold = do single tap
     
  5. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    Oh, after sometime of searching and learning about tapCount and Coroutines I finally got a very simple solution. Works on iOs and Android

    Code (CSharp):
    1. public class DoubleTap : MonoBehaviour
    2. {
    3.     private float doubleTapThreshold = 0.3f;
    4.     private int tapCount;
    5.  
    6.     IEnumerator SingleOrDoubleTap()
    7.     {
    8.         yield return new WaitForSeconds(doubleTapThreshold);
    9.  
    10.         if (tapCount == 1)
    11.         {
    12.             Debug.Log("SingleTap");
    13.             tapCount = 0;
    14.         }
    15.         else if (tapCount == 2)
    16.         {
    17.             Debug.Log("Double Tap");
    18.             tapCount = 0;
    19.         }
    20.  
    21.     }
    22.  
    23.     private void Update()
    24.     {
    25.         if (Input.touchCount == 1)
    26.         {
    27.             if (Input.GetTouch(0).phase == TouchPhase.Ended)
    28.             {
    29.                 tapCount++;
    30.                 StartCoroutine(SingleOrDoubleTap());
    31.             }
    32.         }
    33.  
    34.     }
    35. }