Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by VincentRodriguez · Oct 12, 2012 at 07:35 PM · javascriptmousedouble-click

Double click mouse detection ?

Hello world !

I want to make a double-click function with js. But when I tried to make it, I got messed with Time.time :D. So can anyone help with that ?

Thanks in advance !

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Oct 12, 2012 at 07:42 PM 1
Share

hello double click ...

 timeBetweenDubClick = 0.20 (or whatever)
 when a click happens ...
 gapHere == Time.time - previousClickTime
 if gapHere < timeBetweenDubClick .. that is a doubleclick
 previousClickTime = Time.time

15 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by sparkzbarca · Oct 12, 2012 at 07:50 PM

double click really means 2 clicks within a narrow range of time

 bool one_click = false;
 bool timer_running;
 float timer_for_double_click;
 
 //this is how long in seconds to allow for a double click
 float delay;
 if(input.GetMouseButtonDown(0))
 {
    if(!one_click) // first click no previous clicks
    {
      one_click = true;
      
      timer_for_double_click = time.time; // save the current time
      // do one click things;
    } 
    else
    {
      one_click = false; // found a double click, now reset
      
      do double click things
    }
 }
 if(one_click)
 {
    // if the time now is delay seconds more than when the first click started. 
    if((time. time - time_for_double_click) > delay
 {
 
 //basically if thats true its been too long and we want to reset so the next click is simply a single click and not a double click.
 
     one_click = false;
    
 }


hope that helps! dont forget to mark if answered.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image carlosfelipetorres75 · Dec 11, 2015 at 05:23 PM 0
Share

Excelent answer ! Works perfect!

avatar image Macroblitz · Apr 30, 2019 at 01:19 PM 0
Share
 if (!one_click)
         {
             timer_for_double_click = Time.time;
             one_click = true;
         }
 else
         {
             if ((Time.time - timer_for_double_click) > delay)
             {
                 timer_for_double_click = Time.time;
             } else
             {
                 //Do something here if double clicked
 
                 one_click = false;
             }
 }
avatar image
8

Answer by aphenine · Aug 16, 2016 at 05:04 AM

If you stay with the EventSystem method, you don't need to check timings and do it manually, the event data generated by each pointer press will do it for you.

Step 1: Learn about the EventSystem and EventTriggers scripts:

https://www.youtube.com/watch?v=3NBYqPAA5Es (Lovely YouTube video)

Step 2: For non-UI elements, you can add a EventTrigger to them too, but you also need to add a Physics Raycaster to the camera you're using. Then the stuff outlined in the tutorial above will work for 3D objects (and 2D objects if a 2D game and the 2D Raycaster is used)

Step 3: Get involved with Pointer event data

If you use this system, and you set up a callback in the editor for a pointer click on GameObject, then you can't control things like which mouse button was pressed or (as per the original question) whether it's a single or double click. Your click function will get called whichever button is used, however many time you click.

However, the EventSystem and Input Modules collect all of that stuff by default for you, you just need to get at it. To access it, you just need to do some extra stuff:

http://answers.unity3d.com/answers/1229912/view.html

Hope that helps.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Jan 15, 2018 at 02:37 PM 0
Share

This should be the accepted answer, it is a lot better solution than a custom input detection with Input.GetButtonDown() (and it should actually be GetButtonUp()). Imagine you want to switch over to using controller, not to mention touch control!

avatar image
5

Answer by cenk5355 · Feb 11, 2014 at 02:07 PM

this is tested and working,good luck:


private var lastClickTime:float=0;

var catchTime:float=.25;

function Update () {

 if(Input.GetButtonDown("Fire1")){
     if(Time.time-lastClickTime<catchTime){
         //double click
         print("done:"+(Time.time-lastClickTime).ToString());
     }else{
         //normal click
         print("miss:"+(Time.time-lastClickTime).ToString());
     }
     lastClickTime=Time.time;
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ajdrew81 · Jun 06, 2015 at 01:03 AM 0
Share

almost. if you double click, then keep clicking faster than the catchTime, it registers as a double click for every single successive click, rather than requiring a double click after double clicking.

avatar image
1

Answer by DSobscure · Aug 26, 2016 at 04:48 AM

try to use Event.current.clickCount

such like

 void OnGUI()
 {
     if(Event.current.isMouse && Event.current.button == 0 && Event.current.clickCount > 1)
     {
         Debug.Log(Event.current.clickCount);
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Yaakov_Shahak · Jan 15, 2018 at 01:33 PM

Corutines can be very useful for this purpose:

         private WaitForSeconds doubleClickTreashHold = new WaitForSeconds(1f);
         private int clickCount;
 
         private void Update()
         {
             if (Input.GetMouseButtonDown(0))
             {
                 OnPointerClick();
             }
         }
 
         private void OnPointerClick()
         {
             clickCount++;
             if (clickCount == 2)
             {
                 print("double click!");
                 clickCount = 0;
             }
             else
             {
                 StartCoroutine(TickDown());
             }
         }
 
         private IEnumerator TickDown()
         {
             yield return doubleClickTreashHold;
             if (clickCount > 0)
             {
                 clickCount--;
             }
         }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mechanical_Lions · Jan 20, 2018 at 07:25 AM 0
Share

I modified this slightly to use

  //input mouse click
     clickcount++
     If(clickCount !=2)
     {
     StartCoroutine(TickDown());
     //Do single click stuff
     }
     else
     {
       clickCount = 0;
       print("We double clicking now!");
     }

ins$$anonymous$$d of calling the method OnPointerClick(), just because it did mostly the same thing but I didn't need to rewrite larger blocks of code to impliment. Thank you for the starting point.

  • 1
  • 2
  • 3
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Click to destroy object 2 Answers

Rotate object following mouse movement. Object jumps/ flips 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges