• 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
1
Question by pickledzebra · Jun 16, 2010 at 06:39 PM · double-click

Simultaneous Single and Double Click functions?

I'm having some difficulty figuring out the logic of how to address the following problem (as well as coding it in Javascript).

I have a single-click (mousedrag) function to rotate/zoom an object in my scene.

I also have a double-click "picking" function for objects in my scene. The "picking" function actually does some fairly complicated things in terms of rearranging objects and parent-child relationships.

The issue I'm having is that if one clicks spastically (rapid-fire series of clicks), the things being done by the single-click and double-click functions seem to interefere causing my game object of interest to go flying out of sight.

I'm trying to figure out if I can make these mutually exclusive such that they CANNOT happen simultaneously. So, that either of the clicks in a double-click cannot be interpreted as a single click.

thanks for any insights, pointers or code

Comment
Add comment
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

3 Replies

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

Answer by Tetrad · Jun 16, 2010 at 07:05 PM

The easiest solution is simply not to execute your single click until you know it's a double click.

So you'd have some input code. You detect a click. If the time it takes to register a double click has passed and there's no second click, you do whatever it is you do for a single click. If there's a second click you do what there is for a double click.

Now for dragging objects, you typically don't register for clicks but for mouse down+hold events. You probably would also want to register for some "how long can the button be down before it's a click vs a click+hold" logic.

Comment
Add comment · Show 3 · 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 pickledzebra · Jun 17, 2010 at 12:20 PM 0
Share

Thanks. The logic seems simple, but I'm having difficulty figuring out how to initiate the single-click (mousedrag) without waiting for a second click to verify the time gap is large enough.

avatar image Tetrad · Jun 17, 2010 at 12:21 PM 0
Share

What you could do is start your mouse drag immediately, and if a double click is detected "undo" your mouse drag movement.

avatar image pickledzebra · Jun 17, 2010 at 12:38 PM 0
Share

That might work. I'm wondering if coroutines would work here - though I'm not sure how to implement them either. Perhaps start both, but have a yield statement that will let the double-click check run to completion?

avatar image
0

Answer by jobar · Sep 09, 2012 at 09:00 PM

The following code could be used to determine single click vs double clicks. It uses a timer that starts after the first mouse click. If the user clicks again before a specified time limit we register that as our second mouse click.

C# Global Variables

 private int mouseClicks   = 0;
 private float mouseTimer = 0f;
 private float mouseTimerLimit = .25f;

Next we define a function,

 void checkMouseDoubleClick()
 {
     if(Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0)
     {
         mouseClicks++;
         Debug.Log("Num of mouse clicks ->" + mouseClicks);
     }
     
     if(mouseClicks >= 1 && mouseClicks < 3)
     {
         mouseTimer += Time.fixedDeltaTime;
         
         if(mouseClicks == 2)
         {
             if(mouseTimer - mouseTimerLimit  < 0)
             {
                 Debug.Log("Mouse Double Click");
                 mouseTimer = 0;
                 mouseClicks = 0;
                /*Here you can add your double click event*/
             }
             else
             {
                 Debug.Log("Timer expired");
                 mouseClicks = 0;
                 mouseTimer = 0;
                /*Here you can add your single click event*/
             }
         }
         
         if(mouseTimer > mouseTimerLimit)
         {
             Debug.Log("Timer expired");
             mouseClicks = 0;
             mouseTimer  = 0;
            /*Here you can add your single click event*/
         }
     }
 }

I know the logic can be simplified so feel free to adjust. Again, this function simply checks the time elapsed between the first and second mouse click.

I needed this function for a program in which I could not use Unity's mouseUp/mouseDown functions.

Hopefully this helps someone in the future.

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 geniuscd · Dec 12, 2012 at 02:10 PM 0
Share

i think you cant use deltaTime to actually see how many clicks the user clicked.. it will return a value of ~0.007377... ( i tested it) its not accurate .

avatar image jobar · Jan 25, 2014 at 09:01 PM 0
Share

Thanks geniuscd, I realized there was a typo. I removed the line of code mouseClicks+= Time.deltaTime (typo).

avatar image
0

Answer by superboy007 · Oct 25, 2013 at 11:33 AM

bool mouseClicksStarted = false; int mouseClicks = 0; float mouseTimerLimit = .25f;

 public void OnClick(){
     mouseClicks++;
     if(mouseClicksStarted){
         return;
     }
     mouseClicksStarted = true;
     Invoke("checkMouseDoubleClick",mouseTimerLimit);
 }
 
  
 private void checkMouseDoubleClick()
 {
     if(mouseClicks > 1){
         Debug.Log("Double Clickedd");
         
     }else{
         Debug.Log("Single Clicked");
         
     }
     mouseClicksStarted = false;
     mouseClicks = 0;
 }
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to detect Double Clicking Horizontal/Vertical Axis on joystick 0 Answers

Unity in Mac OS X trackpad clicking count problem. 0 Answers

Double click events 5 Answers

Can I detect a doubleclick on a Gizmo or GameObject? 2 Answers

Differentiate between mouse click and mouse hold 1 Answer


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