I want to call a method whenever I'm holding down RMB, and as long as I'm not hovering over UI. But I also want it so that if I'm holding down RMB and I drag the mouse off the UI, the method doesn't get called. This is what I'm struggling to do. This is how I'm checking for the UI: Code (CSharp): hoveringOverUI = EventSystem.current.IsPointerOverGameObject(); And this is the code that calls the method: Code (CSharp): if (Input.GetMouseButton(1) && hoveringOverUI == false) { // Call method } I'm pretty sure a custom bool for checking RMB would be involved, but every solution I thought of, either ended up being the original, or somehow causing other issues (like never being able to call the method).
You could just add a bool to check if it was pressed when not over the UI. Code (CSharp): private bool _rmbPressedOffUI; if (Input.GetMouseButtonUp(1)) _rmbPressedOffUI = false; if (hoveringOverUI) return; if (Input.GetMouseButtonDown(1)) _rmbPressedOffUI = true; if(Input.GetMouseButtonDown(1) && !_rmbPressedOffUI) { //Call Method }
Doesn't seem to be executing the last check because of "!_rmbPressedOffUI". If I change it to "_rmbPressedOffUI == true" it goes back to acting as it did originally (obviously). Tried to change the whole code in a lot of ways, always ends up acting like the original.
Got it working. Just needed to set the second bool when RMB was pressed, then check against it at the start (before the if check calling return). Thank you for the help. Code (CSharp): if (_rmbPressedOffUI == true) // Call method if (Input.GetMouseButtonUp(1)) _rmbPressedOffUI = false; if (hoveringOverUI) return; // If hovering over UI, stop rest of method code from running if(Input.GetMouseButtonDown(1)) _rmbPressedOffUI = true; // When pressing RMB, set bool to true