- Home /
how to hit a gui button with raycast
i have an item that when dragged, but then clicked it sends ray and if it hit a
collider of a specific tag it does this or that, i want to add gui, if it hit.transform, of gui, to do blahhh not important, but how can i know i hit gui? i have searched alot how to do this but come up alot in searches, of guitext etc unrelated things, but still i apologize if this is simple or been asked/answered somewhere already
Answer by robertbu · Mar 22, 2013 at 06:06 AM
To the best of my knowledge, there no way to cast a ray on a GUI Button...they don't have colliders, they live in the GUI coordinate system (not world coordinates), and they have no Z axis (i.e. you cannot get a perspective view on one). You can test if the mouse is over a particular button:
private var myRect = Rect(100,100, 350, 350);
function OnGUI () {
var e : Event = Event.current;
if (myRect.Contains(e.mousePosition))
Debug.Log("Moving over the button");
if (GUI.Button(myRect, "My button"))
Debug.Log("Button Pressed");
}
thank you! i believe this will help, my rects are draggable etc, so this solution seems it will follow them anyways, my next question is, can this be placed in a call with a raycast? or both of them happening at the same time i mean,
Processing GUI events should be done inside of OnGUI. GUI coordinates are different than Screen coordiantes. That is the position above (e.mousePosition) is not equal to Input.mousePosition. So if you needed to test the rectangles in Update() you would have to translate between the two coordinate systems.
no need for a raycast, note:
raycast takes a RAY, in your case a ScreenPointToRay we assume..
ScreenPointToRay takes a ScreenPosition as IT'S argument..
if (myRect.Contains(e.mousePosition))
this bypasses the need for a ScreenPointToRay, we use screen position directly to just see if the rect contains it!
the raycast is happening in the ongui function, when item1GUI = 1 it creates a little button at mouse point with the texture of an item i picked up, and when i click it performs a raycast to see if it hit terrain or player, so if i throw this in there and it clicks on a button and i say return after i do all my stuff it should just work right? lol i guess testing will answer this question, thanks again
It's not a good idea to put a Raycast inside of OnGUI(). OnGUI() can be called multiple times per frame. So if you want to do the Rect testing in Update() you can make the conversion from GUI to Screen as follows:
var v3Pos = Input.mousePostion;
v3Pos.y = Screen.height - v3Pos.y;
v3Pos is now a position that can be tested against the rectangles you are using in your OnGUI() call.
Answer by Seth-Bergman · Mar 22, 2013 at 06:00 AM
what you want is GUIElement.HitTest
:)
(not relevant with a GUI.Button though, for an actual button you just declare it in the if):
if(otherCriteria){
if(GUI.Button(Rect...etc{
//special button code
}
}
(similar to @robertbu 's answer, the area is simply defined by the Rect)
thanks kindly! if i could submit both as an answer i would lol
Your answer
Follow this Question
Related Questions
Raycasting is not working! 2 Answers
EnumPopup in Javascript? 2 Answers
[SOLVED] Raycast for custom button JS 1 Answer
Raycast intersection 1 Answer
SORPG: Distance problem? 1 Answer