- Home /
OnMouseDown for right mouse click without being hacky?
Hi Unity people!
I'm building a 2D puzzle game with GameObjects that can be clicked on. My GameObjects have a script with the OnMouseDown
method implemented to handle left-clicking. However, I also want to handle right-clicking, which doesn't trigger OnMouseDown
.
I've seen some suggestions that look like the following:
void OnMouseOver() { if (Input.GetMouseButtonDown(1)) { Debug.Log("Right click on this object"); } }
If this is still the best solution in Unity 2017, I can live with it, but all the suggestions I saw were really old (the newest was in 2013), so I'm hoping there's something cleaner available now that I've missed. Anyone got anything?
Not that I'm aware of... I actually only use queries from the Input class for this very reason. It's typically easier to check for a mouse click then raycast to get the selected object.
Answer by elenzil · Sep 28, 2017 at 10:15 PM
Use UGUI's EventTrigger.
override OnPointerDown()
, which gives you a PointerEvent, which contains an InputButton member "button", which tells you left/right/middle.
Does this apply to GameObjects? I might be missing something, but it looks like this is just for GUI stuff.
this is just for GUI stuff
that's true. but maybe you could stick a GUI element over/under the whole scene and when you catch a R$$anonymous$$B click you could do a raycast.