- Home /
Prevent Raycast on reacting to GUI input?
So in the 3d space of my game, raycast from mouse is suppose do something - but is it possible to disable this raycast behavior when the user is interacting with a GUI on top? i.e., to have the GUI be a "layer" to prevent the raycast from going off?
Answer by Nerull22 · Aug 31, 2011 at 12:32 PM
Just for future reference if you supply some scripting examples, that would help us help you more (Does the Tom Cruise laugh).
However, basically I'm guessing that you have the raycast being sent whenever you press the left mouse button, but don't want it sent when the player is interacting with a button or something else on the GUI layer. Basically, you are going to have to put in a check to see what the mouse is currently over to tell the program whether or not it should fire the raycast or not. So something like:
if(Input.Button == "Fire1" && !overGUIElement)
{
//Fire raycast code here.
}
I'm hoping that this will help and is indeed the question tht you were asking. IF it isn't the answer you're looking for, post back with some code for us to look at, and the community will be able to help you more effectively.
This is basically what I am asking, but I am not sure if Unity GUI has any methods for detecting when a mouse is over a GUI button... (as opposed to when it is pressed)
Answer by Nerull22 · Sep 02, 2011 at 12:48 PM
There is, sorry I should've put that in with my answer. It's a function that you can call that is extended from MonoBehaviour. Just put in the function:
void OnMouseEnter(){}
And that function will initiate whenever the mouse if over a GUI element. Also you may want to add some code for when the mouse leaves the GUI element which you can use:
void OnMouseExit(){}
So if you have a boolean in the script to detect whether or not it's over a GUI element or not, then you can just put:
private bool overGUIElement = false;
void OnMouseEnter(){overGUIElement = true;}
void OnMouseExit(){overGUIElement = false;}
So now all you have to do is check against that boolean in your update and you'll be able to judge whether or not the player is hovering over a button or not. Hope that finishes up the question, if not just comment again.
Your answer
Follow this Question
Related Questions
GUI.button and onClick Terrain 2 Answers
Trouble targeting GUI with raycast 0 Answers
Raycast from GUI Camera possible to only effect gui? 1 Answer
Raycast and GUI question 1 Answer
Touch performance: Multiple raycast-scripts or better only one 1 Answer