Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by joeyaaaaa · Mar 22, 2013 at 05:50 AM · guiraycast

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
 }
Comment
Add comment · Show 16 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joeyaaaaa · Mar 22, 2013 at 06:17 AM 0
Share

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,

avatar image robertbu · Mar 22, 2013 at 06:26 AM 0
Share

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.

avatar image Seth-Bergman · Mar 22, 2013 at 06:29 AM 0
Share

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!

avatar image joeyaaaaa · Mar 22, 2013 at 06:31 AM 0
Share

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

avatar image robertbu · Mar 22, 2013 at 06:41 AM 1
Share

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.

Show more comments
avatar image
0

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)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joeyaaaaa · Mar 22, 2013 at 06:15 AM 1
Share

thanks kindly! if i could submit both as an answer i would lol

avatar image Seth-Bergman · Mar 22, 2013 at 06:18 AM 0
Share

Just as long as you accept one correct one!

avatar image joeyaaaaa · Mar 22, 2013 at 06:24 AM 0
Share

^^ lol thanks again

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges