Any idea why this:
private void OnMouseDown()
{
if (Input.GetMouseButton(0)) //Left mouse button click
{
Debug.Log("Left Mouse Button Pressed");
}
else if (Input.GetMouseButton(1)) //Right mouse button click
{
Debug.Log("Right Mouse Button Pressed");
}
else if (Input.GetMouseButton(2)) //Middle mouse button click
{
Debug.Log("Middle Mouse Button Pressed");
}
}
doesn’t work for right and middle button, but it works fine for left button?
try taking the ‘else’ out from the 6th and 10th line.
moving the second two into the Update() worked, and since this is what I needed anyway that solved it for me. Not sure why they would not fire in the OnMouseDown() - maybe a bug?
Nope, OnMouseDown is to see if an object has been clicked on (with the left button), not for generally checking mouse buttons.
–Eric
You can fake an OnMouseDown for the right and middle buttons though.
void OnMouseOver()
{
if (Input.GetMouseButtonDown(1))
{
Debug.Log ("Right Mouse Button Pressed");
}
}
Hi,
I am trying to detect a single right mouse click but using the code shown by dawvee produces lots of clicks.
How can this be adapted to detect individual right clicks?
Thanks
Geoff
How would right mouse click wok in web browser…does int pull up the options?
Im also interested in one that registers
a continual mouse down. Instead of a click it looks to see if mouse button is still down.
Gaba
August 24, 2009, 2:37am
9
@Geoff and Warrior
there’s two scripts for different mouse events
they are
Input.GetMouseButton
Returns whether the the given mouse button is held down.
Input.GetMouseButtonDown
Returns true during the frame the user pressed the given mouse button.
So, you can use each of these depending on your necessity k?
For more info on the methods, you con check the scripting reference for them
also, you can select how the right click will behave on a browser on the project settings, not sure exactly where, but i know it can be done
Hope i helped
Don’t use getMouseButton but use the regular “for all key working” functions. there are currently some rumored problems with the GetMouseButton on real 3+ button mice especially windows