Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Apr 27, 2018 at 11:08 AM by RX187 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by RX187 · Apr 12, 2018 at 11:03 AM · tutoriallearning

Detect specific game object with raycast and show UI when hit

Hello,

So I am trying to make a light switch ( among other things ) work using raycast. I made it work, but the problem is that I don't know how I can also show UI like a tool tip ("Press F to use") only when the mouse is over the game object (the light switch). The range works but when I added the text it shows everywhere. Is the raycast hitting everything that's in the mouse position range?

public GameObject lightSwitch;
public GameObject lightSource;

public Text text;

private bool isOn;

 void Start()
 {
     lightSource.SetActive(false);
 }

 void Update()
 {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, 1))
     {
         text.text = "PRESS 'F' TO USE";
         if(Input.GetKeyDown(KeyCode.F) && !isOn)
         {
             isOn = true;
             lightSource.SetActive(true);
         }
         else if(Input.GetKeyDown(KeyCode.F) && isOn)
         {
             isOn = false;
             lightSource.SetActive(false);
         }
     }
 }

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

  • Sort: 
avatar image
0
Best Answer

Answer by RX187 · Apr 13, 2018 at 12:27 PM

Thanks for the info but I actually found a solution:


 public GameObject lightSwitch;

 public GameObject lightSource;

 public Text text;

 private bool isOn = false; 

 public Collider col;

 void Start()
 {
     lightSource.SetActive(false);

     col = GetComponent<Collider>();
 }

 void Update()
 {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if(col.Raycast(ray, out hit, 1 ))
     {
         text.text = "Press F to use";
         if(Input.GetKeyDown(KeyCode.F) && !isOn)
         {
             
             isOn = true;
             lightSource.SetActive(true);
         }
         else if(Input.GetKeyDown(KeyCode.F) && isOn)
         {
             isOn = false;
             lightSource.SetActive(false);
         }
     }
     else { text.text = null; }
 }
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
avatar image
1

Answer by tormentoarmagedoom · Apr 12, 2018 at 01:49 PM

Good day.

I recommend you to learn abaut Event Trigger system. You will see is super simple to use, and so more easy than using Raycasts. And was created exactly for what you need.

Is better for you to go in an look the tutorial, better than recieve a specific answer ofr an specific problem.


If helped, accept the answer and close the question! Bye!

Comment
Add comment · Show 1 · 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 micropowermantenimiento · Apr 12, 2018 at 03:01 PM 1
Share

Agree. Event Trigger is best option

Follow this Question

Answers Answers and Comments

138 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Is the customizable character tutorial still out there somewhere? 2 Answers

Unity 3D textbook / ebook 0 Answers

Learning resource for networking? 1 Answer

Looking for a turn-based strategy beginner's tutorial. 5 Answers

Tutorial level 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