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 NeinZisIsFromMeinSpringCollectionJa · Sep 19, 2014 at 02:16 PM · guiraycast

How would I bring up a GUI after a raycast has hit a gameObject with a specific tag?

I've got the raycast to hit the gameObject and print a message if the tag == "Vendor". However I tried to create a GUI.Box and it didn't work, I'm doing this under OnGUI() also.

I'm a bit hesitant to post my code as I have only just started, so don't be too harsh if my code isn't up to scratch, haha.

Anyway, this is what I have so far:

 void OnGUI ()
     {
         if(Input.GetKeyUp (KeyCode.E))
         {
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             RaycastHit hit;
             if (Physics.Raycast(transform.position, fwd, out hit, 2))
             {
                 if(hit.collider.gameObject.tag == "Vendor")
                 {
                     GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 200), "Vendor's Inventory");
                 }
             }
         }
     }
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 dsada · Sep 19, 2014 at 02:47 PM

This is not the right direction. The raycast part is okay, but you are using it badly. The first thing you need to know is that Input functions only work well in the Update() function. The second thing that you need to know is that if you would like to show some GUI related things, you need that call everytime OnGUI is called. Imagine those things like local variables. So to satisfy both restrictions you need to transform your script like this:

 private bool hitSuccess = false;
 
 void Update()
 {
     if(Input.GetKeyUp (KeyCode.E))
         {
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             RaycastHit hit;
             if (Physics.Raycast(transform.position, fwd, out hit, 2))
             {
                 if(hit.collider.gameObject.tag == "Vendor")
                 {
                     hitSuccess = true;
                 }
             }
         }
 }
 
 void OnGUI ()
 {
         if(hitSuccess)
         {
            GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 200), "Vendor's Inventory");
         }
 }
Comment
Add comment · Show 2 · 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 NeinZisIsFromMeinSpringCollectionJa · Sep 19, 2014 at 10:39 PM 0
Share

Thank you very much man! This worked like a charm, and I also edited it to allow a button to close the window using the bool as well. Thank you again!

avatar image Shane912 · May 14, 2015 at 01:32 PM 0
Share

NeinZisIsFrom$$anonymous$$einSpringCollectionJa, would this be the appropriate method to use if I wanted to look at an object in the distance, then bring it up on a GUI to list the text written on it?

avatar image
0

Answer by dmg0600 · Sep 19, 2014 at 03:02 PM

It's not that it doesn't work, it is that the box is only displayed for one frame when the GetKeyUp triggers.

You should use a local bool variable to check if the box should be displayed. That way it will be displayed when the variable is true and not only for one frame.

It is better, as dsada pointed out to use the Input and Raycast part of the script in Update. The thing that is missing in that script is to toggle the box off.

 void Update () {
     if (Input.GetKeyUp(KeyCode.E))
     {
         showBox = false;
 
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         RaycastHit hit;
         if (Physics.Raycast(transform.position, fwd, out hit, 2))
         {
             Debug.Log("Choco con " + hit.collider.name);
             if (hit.collider.gameObject.tag == "Vendor")
             {
                 showBox = true;
             }
         }
     }
 }
 
 void OnGUI()
 {
     if (showBox)
         GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 200), "Vendor's Inventory");
 }

Setting the variable to false at the beggining of the if, the box will toggle off whenever you press E and the Raycast doesn't hit the vendor.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnMouseUp() not working like meant 1 Answer

Raycast shooting in the middle of the screen 0 Answers

GUI label on raycast? 0 Answers

Raycast behavies as if its snapping to grid on standalone build 1 Answer

Trouble targeting GUI with raycast 0 Answers


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