x


How can i Double click on a gameObject?

I'm trying to enable double clicking on my gameobject to rotate it by a preset number of degrees, Looking in the docs it appears that the Event.current.clickCount property is just for use with GUI.

more ▼

asked Dec 07 '09 at 09:40 PM

davebuchhofer gravatar image

davebuchhofer
492 7 11 24

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Here's a somewhat less complicated way:

using UnityEngine;

public class DoubleClick : MonoBehaviour {

    float doubleClickStart = 0;

    void OnMouseUp()
    {
        if ((Time.time - doubleClickStart) < 0.3f)
        {
            this.OnDoubleClick();
            doubleClickStart = -1;
        }
        else
        {
            doubleClickStart = Time.time;
        }
    }

    void OnDoubleClick()
    {
        Debug.Log("Double Clicked!");
    }

}

Note, the double-click sensitivity is determined by the 0.3f float value. This is the amount of time allowed between clicks to register a double click.

Also, the start time is re-set to a negative number when a double click is triggered, so that three clicks in quick succession does not trigger two double click events - they're only triggered in pairs.

more ▼

answered Dec 08 '09 at 09:45 AM

duck gravatar image

duck ♦♦
40.9k 92 148 415

btw, where did u define the mouse button you use?

Feb 08 '11 at 07:02 AM lisor15
(comments are locked)
10|3000 characters needed characters left

I have one semi solution here which works currently, but the trapDoubleClicks function is called again while triggering the double click. There's got to be an easier way for this, but i couldn't find it in my searching!

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Collider))]
public class DoubleClickRotateObject : MonoBehaviour
{
    public float DoubleClickTimeout = 0.2f;
    public Vector3 DoubleClickRotateAmount = new Vector3(0, 0, 15);

    public bool PrintDebug=false;

    void OnMouseUp()
    {
       if (PrintDebug) print("mouse up on " + transform.name);
       StartCoroutine(trapDoubleClicks(DoubleClickTimeout));
    }

    IEnumerator trapDoubleClicks(float timer)
    {
        if (PrintDebug) print("Starting to listen for double clicks");
        float endTime = Time.time + timer;
        while (Time.time < endTime)
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (PrintDebug) print("Double click!");
                transform.Rotate(DoubleClickRotateAmount);
            }
            yield return 0;
        }
        if (PrintDebug) print("no double click in " + timer + " amount of time");
        yield return 0;
    }
}
more ▼

answered Dec 07 '09 at 09:42 PM

davebuchhofer gravatar image

davebuchhofer
492 7 11 24

(comments are locked)
10|3000 characters needed characters left

Solved

private bool clickEnable = true;
private bool doubleClick = false;

void OnMouseUp ()
            {
               if(clickEnable)   {
             clickEnable = false;
             StartCoroutine(trapDoubleClicks(DoubleClickTimeout));
           }

        }

IEnumerator trapDoubleClicks(float timer)
        {
            Debug.Log("Starting to listen for double clicks");
            float endTime = Time.time + timer;
            while (Time.time < endTime)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    Debug.Log("Double click!");
              yield return new WaitForSeconds(0.4f);
              clickEnable = true;
              doubleClick = true;

                }
                yield return 0;
            }

           if(!doubleClick){
            Debug.Log("no double click in " + timer + " amount of time");
             //single click
           }
           else{
             doubleClick = false;
           }

           clickEnable = true; 
            yield return 0;
        }
more ▼

answered Feb 17 '12 at 06:47 AM

itmind gravatar image

itmind
1

(comments are locked)
10|3000 characters needed characters left

Solved

public float DoubleClickTimeout = 0.2f;
private bool clickEnable = true;
private bool doubleClick = false;

void OnMouseUp ()
    {
       if(clickEnable)   {
         clickEnable = false;
         StartCoroutine(trapDoubleClicks(DoubleClickTimeout));
       }

    }


IEnumerator trapDoubleClicks(float timer)
   {
        Debug.Log("Starting to listen for double clicks");
        float endTime = Time.time + timer;
        while (Time.time < endTime)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("Double click!");
          yield return new WaitForSeconds(0.4f);
          clickEnable = true;
          doubleClick = true;

            }
            yield return 0;
        }

       if(!doubleClick){
        Debug.Log("Single click");

       }
       else{
         doubleClick = false;
       }

       clickEnable = true; 
        yield return 0;
    }
more ▼

answered Feb 17 '12 at 06:47 AM

itmind gravatar image

itmind
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x982

asked: Dec 07 '09 at 09:40 PM

Seen: 6938 times

Last Updated: Feb 17 '12 at 06:47 AM