Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

differencing short mouse click from long mouse click

Discussion in 'Scripting' started by francksitbon, Feb 13, 2011.

  1. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    hi, i've a problem to make difference between a short mouse click and a long one.

    here is my code:

     
  2. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    I also tried the simple code in the function update():

    but it seems that unity does not make difference, starting MouseButtonDown and MouseButton at the same time.
    What i need, is to wait 0.2s during a click before doing stuff from the long click
     
  3. Krobill

    Krobill

    Joined:
    Nov 10, 2008
    Posts:
    282
    Your conditions are wrong I think. Try this :
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     if ( Input.GetMouseButtonDown (0) )
    5.     {
    6.            temps = Time.time ;
    7.     }
    8.  
    9.     if ( Input.GetMouseButtonUp(0)  (Time.time - temps) < 0.2 )
    10.    {
    11.         // short Click
    12.    }
    13.  
    14.     if ( Input.GetMouseButtonUp(0)  (Time.time - temps) > 0.2 )
    15.    {
    16.         // Long Click
    17.    }
    18. }
    19.  
    Untested but it should work...

    You don't need the click Boolean if you use GetMouseButtonUp and GetMouseButtonDown instead of GetMouseButton. In your code the main mistake was to reset temps every frame when the mouse button was down instead of only when the button was actually pressed.
     
    hof13nn and Studious_Gluteus like this.
  4. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    Yes but i need to do something during the long click and not after.

    if ( Input.GetMouseButtonUp(0) (Time.time - temps) > 0.2 )
    {
    // Long Click
    }
    should work only after a long click and not during the long click as i need.
     
  5. Krobill

    Krobill

    Joined:
    Nov 10, 2008
    Posts:
    282
    Ok then ...

    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     if ( Input.GetMouseButtonDown (0) )
    5.     {
    6.            temps = Time.time ;
    7.            click = true ;
    8.            longClickDone = false ;
    9.     }
    10.  
    11.     if ( click  !longClickDone  (Time.time - temps) > 0.2 )
    12.    {
    13.         // long click effect
    14.         longClickDone = true ;
    15.    }
    16.  
    17.     if ( Input.GetMouseButtonUp(0) )
    18.    {
    19.        click = false ;
    20.        if ( (Time.time - temps) < 0.2 )
    21.        {
    22.             // short click effect
    23.        }
    24.    }
    25. }
    26.  
    Still untested... but should do the trick...

    You might not need the longClickDone Boolean, if your long click effect must occur every update while the button is still pressed...
     
    Last edited: Feb 13, 2011
    SheZii likes this.
  6. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    tried, but does not work, it seems that longClickDone, is never set to true to enter the second if.
     
  7. Krobill

    Krobill

    Joined:
    Nov 10, 2008
    Posts:
    282
    !longClickDone is equivalent to longClickDone == false so there's no need for longClickDone to be set to true to enter the second if...

    Is your long click effect something that happen just once or over time ?
     
    Last edited: Feb 13, 2011
  8. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    it's strange, seems that Input.GetMouseButtonDown (0) (short) and Input.GetMouseButton (0) (long) are both true at the same Time.time.
     
  9. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    yes but nothing happens when i trie your code for the long click
     
  10. Krobill

    Krobill

    Joined:
    Nov 10, 2008
    Posts:
    282
    In my code, I never use GetMouseButton(0)...
     
  11. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    because as soon as longClickDone = true ; in the second if, it exits it. I use the left click hold to move the character controller in update. the player doesnot move because of longClickDone = true in the second if. if i delete this line, it moves, but the short click not.
     
  12. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    in mine, yes ;)
     
  13. Krobill

    Krobill

    Joined:
    Nov 10, 2008
    Posts:
    282
    Well... i can't help you if you pick some of my code, mingle it a bit with yours and just tell me it doesn't work with no further explanations...

    I told in you my second answer that you might not need the longClickDone Boolean, if your long click effect must occur every update while the button is still pressed. That's obviously the case. So...

    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     if ( Input.GetMouseButtonDown (0) )
    5.     {
    6.            temps = Time.time ;
    7.            click = true ;
    8.     }
    9.  
    10.     if ( click  (Time.time - temps) > 0.2 )
    11.    {
    12.         // long click effect
    13.    }
    14.  
    15.     if ( Input.GetMouseButtonUp(0) )
    16.    {
    17.        click = false ;
    18.  
    19.        if ( (Time.time - temps) < 0.2 )
    20.        {
    21.             // short click effect
    22.        }
    23.    }
    24. }
    25.  
     
    Deleted User likes this.
  14. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    does not work for short click

    it seems that (Time.time - temps) < 0.2 never happens
    i tried using print (Time.time - temps), its increasing ever, as if temps = 0.... strange no ?
     
  15. Krobill

    Krobill

    Joined:
    Nov 10, 2008
    Posts:
    282
    I just copy-pasted my previous code in a script. It works. So I guess my help ends here ^^
     
  16. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    here is the print("click start" + (Time.time -temps)) return for a simple click: (i put click start, in the first if, the click hold in the second if, and click exit in the third)


    click start0
    click hold20.55229
    click exit20.55229
     
  17. dissid

    dissid

    Joined:
    Dec 16, 2010
    Posts:
    41
    I'd use while(...) control structure for mouse check, and timer tests inside it.
     
  18. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    Yoooohooooo it worked!!!!!!!!!!!!!!!!!!!

    actually i forgot this at the beginning :

    var temps: float;

    I don't know why, but unity did not print the error !!!!
    Argh i lost the afternoon on it.
    Thanks a lot Krobill

    Here is my final code to put in the function update : and don't forget the "private var temps: float;" at the begining lol
     
    Fusionayy likes this.
  19. Fusionayy

    Fusionayy

    Joined:
    Aug 2, 2015
    Posts:
    1
    Thank you guys, this was load of help
     
  20. innsofheaven

    innsofheaven

    Joined:
    Aug 28, 2014
    Posts:
    1
    How can i like fill power with mouse click? Like 8 ball games.I clicking mouse button filling power bar.
     
  21. SamRock

    SamRock

    Joined:
    Sep 5, 2017
    Posts:
    250
    Thank you! Works great