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.
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.
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.
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.