Search Unity

detect double click - more reliable solution than this?

Discussion in 'Scripting' started by newborne, Sep 14, 2012.

  1. newborne

    newborne

    Joined:
    Jul 16, 2012
    Posts:
    31
    Hi! I have this code that i am using to detect single and double clicks, but i guess it could be done better with coroutines and further make it more reliable. any suggestions?

    Code (csharp):
    1.  
    2.  
    3.         public bool sglClick;
    4.     public bool dblClick;
    5.     public float duration = 0.1f;
    6.     public float startTime;
    7.  
    8.         void Update()
    9.     {
    10.  
    11.         if(sglClick)
    12.         {
    13.             if(Time.time - startTime > duration)
    14.             {
    15.                 sglClick = false;
    16.                 Debug.Log("single tap");
    17.             }
    18.         }
    19.     }
    20.    
    21.     void OnTapHandler(Vector2 pos)
    22.     {
    23.  
    24.         if(sglClick)
    25.             dblClick = true;
    26.        
    27.         sglClick = true;
    28.        
    29.         if(sglClick)
    30.         {
    31.             if(!dblClick)
    32.             {
    33.                 startTime = Time.time;
    34.             }
    35.             else
    36.             {
    37.                 if(Time.time - startTime < duration)
    38.                     Debug.Log("double tap");
    39.                
    40.                 sglClick = false;
    41.                 dblClick = false;
    42.             }
    43.         }
    44.     }
    45.  
    it's messy but it works
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    How about just using Touch.tapCount, or Event.clickCount?

    --Eric
     
  3. newborne

    newborne

    Joined:
    Jul 16, 2012
    Posts:
    31
    i am using third party asset fingergestures that cannot distinct double from single taps easily and i have to have a workaround!
    it is easy to use and has some good features but can't do this :(

    i am avoiding OnGUI, and i believe that Event is used in OnGUI so i would not use that, and that is only for mouse clicks i think.

    that is why i am looking for something like this.
    any help with this? i would appreciate it! thank you!