Removing double click

Hi everyone,

I want to make transition from Idle to other animation through single click, also count clicks. However while transition works more or less, the count registers 2 clicks after one mouse button press. Any advices? Code I’m working with:

public class KnightController : MonoBehaviour {
    //Rigidbody2D rigidBody2D;
    Animator anim;
    int count = 0;
    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void Update()
    {
        OnMouseOver();
    }

    private void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0))
        {
            anim.SetBool("IsBeingClicked", true);
            count++;
            print(count);
            StartCoroutine(Wait());
        }
        else
        {
            anim.SetBool("IsBeingClicked", false);
        }
    }
    IEnumerator Wait()
    {
        yield return new WaitForSeconds(.6f);
    }

}

What is the point of the coroutine?
The rest actually looks fine from what I can see.

I wanted to delay time between clicks so count would only register single click.

Ok so then do something like this:

public class KnightController : MonoBehaviour {
    //Rigidbody2D rigidBody2D;
    Animator anim;
    int count = 0;
    bool canClick = true;
    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
    }
  
    // Update is called once per frame
    void Update()
    {
        OnMouseOver();
    }

    private void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0) && canClick)
        {
            anim.SetBool("IsBeingClicked", true);
            canClick = false;
            count++;
            print(count);
            StartCoroutine(Wait());
        }
        else
        {
            anim.SetBool("IsBeingClicked", false);
        }
    }
    IEnumerator Wait()
    {
        yield return new WaitForSeconds(.6f);
        canClick = true;
    }

}

[/QUOTE]

1 Like

Now everything works like it should be. Thank you so much!

1 Like