I have recently had the great opportunity to accept a job making video games for a professor at my university. In working on the current game project for him, I was looking for a piece of code to handle "double tap" interaction for, in this case, the afterburner of a space fighter. Among all the different snippets of code I found through my googling, most of them were very basic, too complicated, convoluted, or just didn't work.
I finally found this one and I feel it should be repeated so more people can find and use it. I take no credit for this bit of code. I found it on the Unity3D forums posted by Chris HG. This code allows the use of 3 different states based on how a single key is used. (Single click, long click, or double click) The code was originally intended for use with clicking a mouse button, but can be easily modified to use any other input.
A few additional notes: this code is written in C# for Unity3D but should be usable with any language or engine with little modification.
_buttonDownPhaseStart and _doubleClickPhaseStart will both need to be defined in your code as floats.
I finally found this one and I feel it should be repeated so more people can find and use it. I take no credit for this bit of code. I found it on the Unity3D forums posted by Chris HG. This code allows the use of 3 different states based on how a single key is used. (Single click, long click, or double click) The code was originally intended for use with clicking a mouse button, but can be easily modified to use any other input.
A few additional notes: this code is written in C# for Unity3D but should be usable with any language or engine with little modification.
_buttonDownPhaseStart and _doubleClickPhaseStart will both need to be defined in your code as floats.
if(Input.GetMouseButtonDown(0))
{
_buttonDownPhaseStart = Time.time;
}
if (_doubleClickPhaseStart > -1 && (Time.time - _doubleClickPhaseStart) > 0.2f)
{
Debug.Log ("single click");
_doubleClickPhaseStart = -1;
}
if( Input.GetMouseButtonUp(0) )
{
if(Time.time - _buttonDownPhaseStart > 1.0f)
{
Debug.Log ("long click");
_doubleClickPhaseStart = -1;
}
else
{
if (Time.time - _doubleClickPhaseStart < 0.2f)
{
Debug.Log ("double click");
_doubleClickPhaseStart = -1;
}
else
{
_doubleClickPhaseStart = Time.time;
}
}
}


