- Home /
Yet Another GUI vs. Raycasting question.
I am moving forward with my dungeon/housing creation system and have come across another issue, I have read a few ways on here regarding making gui ignore raycast. The method I was attempting was somewhat as follows:
if (Event.current != null && Event.current.isMouse)
{
ignoringRaycast = true;
Debug.Log("Ignoring Raycast - true");
}
else {
ignoringRaycast = false;
//Debug.Log("Ignoring Raycast - false");
}
It worked great for my background gui elements, but buttons would still fire through on clicking them. I have read about using a GUI Texture behind it but have yet to see any examples. Also had no like trying the tooltip method - I was hoping to be pointed in the right direction or hopefully someone has a working project that tackles this?
I can post the code if necessary although I removed everything that had to utilize this, it is quite large and tangles between many different scripts so will be quite a bit of work just to make readable.
I've had some advice to use "Event.current.Use();" within the if blocks of the buttons, this does not work either.
I have also had a comment on this regarding what i actually need to do.. basically.. If clicking a button, ignore raycast.. which will ignore the preinstantiate and creation.. sorry if i was vague.. if any more inf is needed please ask
another update, i have not also tried GUILayer.HitTest(); but it as well always thinks the mouse is over the GUI layer no matter where it is on screen.. Still no other responses? please, if i need to clarify the issue any more, just ask what you need from me. thanks.
I run your game, but I didn't understand exactly what you want to do. Do you want to click on the terrain even under the buttons? Or do you want to stop raycasting when the mouse pointer is over the buttons?
Stop raycasting when over the buttons or all GUI elements.
Answer by aldonaletto · Jun 13, 2011 at 02:48 AM
You can define two rectangles (one for each menu) in pixel coordinates, and disable raycasting when the mouse pointer is inside any of these rectangles:
var rMenu1 = Rect(20,200,180,400);
var rMenu2 = Rect(100,80,500,170);
function Update(){
...
var mousePos = Input.mousePosition;
ignoringRaycast = rMenu1.Contains(mousePos) || rMenu2.Contains(mousePos);
...
I'm assuming that ignoringRaycast == true disables raycasting.
If you're using GUI.Box to draw the menus, rMenu1 and rMenu2 will be the same rects that define the vertical and horizontal menus - or you can even define bigger rects, so raycasting will be disabled when the mouse gets near to the menus.
great.. I knew it was simple, I was just looking in every wrong direction. Very greatly appreciated.
Answer by JasonM · Jul 22, 2011 at 06:07 PM
Your solution works, but for if you are using a GUILayout window (like one that can be dragged around) youll find that the y position is wrong. heres my quick fix if someone is still struggling
var windowRect = new Rect(0,0,200,200);
mousePos.x = Input.mousePosition.x;
mousePos.y = Screen.height - Input.mousePosition.y;
ignoreRaycast = windowRect.Contains(mousePos);
Your answer
![](https://koobas.hobune.stream/wayback/20220612103236im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Smart Crosshair 4 Answers
Get GUITexture to follow ScreenPointToRay ray cast 1 Answer
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
How will i get animated gif images in scene? 6 Answers
GUI texture touch input problem 1 Answer