- Home /
Stop RayCast Passing thru GUI
I have a GUI where players select what they want to place in the game world. Once selected a ScreenPointToRay places the object in the world on mouse click.
The problem is that when you click the button, the ray passes thru the GUI and places the object in the world under the button.
Is there a way to stop the ray passing thru the GUI?
Answer by whydoidoit · Jun 03, 2012 at 08:42 AM
For this answer i presume you are using OnGUI and that isnt going to detect your raycast so it Sounds to me like you need a simple state machine for placing the items. When the button is clicked set a Boolean value = true then don't do your ray cast until you find that value. Reset it to flase when you have placed your object. I presume at the moment the raycast is always happening.
Yeah, the OnGui works wonders for everything but this. I have a work around, I am not to happy about though.
I have just tried putting an object in front of the camera but that did not seem to work.
Having spent hours trailing the forums it has been an issue that has been brought up since unity 2.0. People were hoping when OnGUI was introduced this would be fixed but sadly not.
For me it works. But I reset this value at the end of Update() after checking it.
Answer by GuyTidhar · Jun 03, 2012 at 07:00 AM
You should be using a layer to define the GUI object that you do not want to raycast against.
This way, when you do the raycast, you will tell unity to exclude any object that is set with that layer.
Layer mask:
http://unity3d.com/support/documentation/Components/Layers.html
Raycast:
http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
Sorry been at it for 16 hours, So I may not be explaining things correctly. I dont want it to pass thru GUI the raycast should be stopped by the GUI.
You want to test the raycast only against the gui?
If so you just need to use the 'Raycast()' method of a collider in the shape (or bounding shape) of the gui element against which you wish to check for ray hit (collision between the ray from the screen and the collider of the gui):
http://unity3d.com/support/documentation/ScriptReference/Collider.Raycast.html
Answer by fafase · Jun 03, 2012 at 08:50 AM
The way I get it, you wants the gui to lay in the corner and be able to click on it any time and place the object without any other manipulations.
Maybe you could try to check the position of the mouse pointer on the screen and when clicking if it is at a certain position (gui position), then you have the gui options, any other position you are placing on the world using raycast.
Answer by FadeToBlack · Jun 03, 2012 at 01:18 PM
check this question http://answers.unity3d.com/questions/120980/unitygui-click-event.html#answer-261746 i made it through GUIUtility.hotControl