1
\$\begingroup\$

I'm wondering if there is a way to have an event occur when the left mouse button is held down, but not to occur when it is initially clicked. I think you can do something like...

if (Input.GetMouseButtonDown(0))
{
    // do stuff
}
else if (Input.GetMouseButton(0))
{
    // do other stuff
}

It looks at first like this would work because when left click is clicked it would execute the first if statement and not test for the second. And if left click is held it would fail the first if condition. But, does this in fact work? Do you actually enter the second if statement when attempting to click because you actually hold it down for slightly longer than a true click since this is happening in Update?

Now, I want to do this in one condition. Something like...

if (!Input.GetMouseButtonDown(0) && Input.GetMouseButton(0))
{
    // do the stuff that should happen when mouse is held but not clicked
}

but I don't think this works either. Any pointers?

For Context, I want to have something happen in every case when mouse button is held down, and only happen in some cases when mouse button is clicked.

if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject() 
|| !Input.GetMouseButton(0) && Input.GetMouseButton(0))

This doesn't work though

\$\endgroup\$
2
\$\begingroup\$

I don't use Unity, but I'll give it my best shot. One way to approach this problem is by defining a counter variable, that increments each tick of the game that the mouse is held down. Then, you could use the following logic (in pseudo-code):

if (mouseIsDown && timeSinceMouseDown > 0) {
    // held down and not just clicked
}
\$\endgroup\$
0
\$\begingroup\$

Input.GetMouseButtonDown() returns true when the click begins.

Input.GetMouseButtonUp() returns true when the click ends.

Input.GetMouseButton() returns true if the button is being clicked.

So you need to use the third function.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.