- Home /
Anyone have a double-click script more reliable than mine?
Briefly, this script performs poorly enough to be non-viable. Any ideas for improvement or alternative approaches would be much appreciated.
var doubleClickStart = 0;
function OnMouseUp() {
if ((Time.time - doubleClickStart) < 0.4)
{
this.OnDoubleClick();
doubleClickStart = -1;
}
else
{
doubleClickStart = Time.time;
}
}
function OnDoubleClick() {
// do some stuff.
}
$$anonymous$$inor point - consider using boolean variables, ins$$anonymous$$d of float.
Answer by Mike 3 · Jun 11, 2010 at 12:23 AM
looks like you have a few minor bugs there, hopefully this will be a bit better
//start doubleClickStart as a float so no ambiguity to the compiler var doubleClickStart : float = -1.0; //EDIT TO DISABLE MOUSE CLICKS FOR A TIME AFTER DOUBLE CLICK var disableClicks = false; //END EDIT
function OnMouseUp() { //EDIT TO DISABLE MOUSE CLICKS FOR A TIME AFTER DOUBLE CLICK if (disableClicks) return; //END EDIT
//make sure doubleClickStart isn't negative, that'll break things
if (doubleClickStart > 0 && (Time.time - doubleClickStart) < 0.4)
{
this.OnDoubleClick();
doubleClickStart = -1;
lockClicks();
}
else
{
doubleClickStart = Time.time;
}
}
//EDIT TO DISABLE MOUSE CLICKS FOR A TIME AFTER DOUBLE CLICK function lockClicks() { disableClicks = true; yield WaitForSeconds(0.4); disableClicks = false; } //END EDIT
function OnDoubleClick() {
// do some stuff.
}
Briliant! Perfect. Thanks for the corrections and the rapid response.
$$anonymous$$ike, if you happen to read this, how would you ignore a 3rd rapid-fire click?
Great! Again, better than the one I tried to make. I'm learning... Thanks again. :)
Answer by adnirjhar · Jan 29, 2014 at 09:38 PM
C# version->
private float doubleClickStart = -1.0f;
private bool disableClicks = false;
void OnDoubleClick()
{
Debug.Log("Double Click");
// do some stuff.
}
IEnumerator lockClicks()
{
disableClicks = true;
yield return new WaitForSeconds(0.4f);
disableClicks = false;
}
void Update()
{
if (Input.GetMouseButtonUp(0))
{
if (disableClicks)
return;
if (doubleClickStart > 0 && (Time.time - doubleClickStart) < 0.4)
{
this.OnDoubleClick();
doubleClickStart = -1;
lockClicks();
}
else
{
doubleClickStart = Time.time;
}
}
}
Answer by Jemaze · Sep 03, 2017 at 04:01 PM
Try This One! I Created A simple double click script. I hope it Helps!
public bool firstClick=false;
public bool secondClick = false;
public int pressCount = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(firstClick == true)
{
Debug.Log("Start Of Coroutine");
StartCoroutine(doubleClickTime());
}
else
{
pressCount = 0; // the press count will reset after the end of the coroutine
}
}
public IEnumerator doubleClickTime()
{
Debug.Log("Im Counting");
yield return new WaitForSeconds(0.23f);
if(pressCount >=2)
{
Debug.Log("Double Click!");
// Do Some Awesome Things!
}
firstClick = false;
}
public void onClick()
{
firstClick = true;
pressCount++;
}
Your answer
Follow this Question
Related Questions
How to detect Double Clicking Horizontal/Vertical Axis on joystick 0 Answers
Mobile equivilent of onMouseUp 2 Answers
OnMouseOver Problem 3 Answers
Unity in Mac OS X trackpad clicking count problem. 0 Answers
OnMouseUP sometimes doesn't work 1 Answer