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 Feb 04, 2016 at 09:09 AM by AmazingDeveloper2 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AmazingDeveloper2 · Feb 03, 2016 at 08:57 PM · c#unity 5raycast

Can't use Raycast to detect if I look at object

I have tv with 3 buttons and 3 screens. Each button disable all screens and then enable it's own screen. Button is empty GP with the box collider only. I want to use a Raycast to detect when I look at Button1, Button2 or Button3. And after pressing LMB I want to enable one of 3 screens. But my code doesn't work...

alt text

 public GameObject screen1;
 public GameObject screen2;
 public GameObject screen3;

 private Collider thisCollider;
 private int channelNumber;

 void Start ()
 {
     thisCollider = GetComponent<Collider>();
 }
 
 void Update ()
 {
     if (Input.GetButton("Fire1"))
         if (DoPlayerLookAtButton())
             ChangeChannel();
 }

 bool DoPlayerLookAtButton()
 {
     RaycastHit _hit;
     Ray _ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
     bool isHit = Physics.Raycast(_ray, out _hit, 1.5f);
     
     if (isHit && _hit.collider == thisCollider) // with the second condition it doesn't work
     {
         switch (thisCollider.name)
         {
             case "Button1": channelNumber = 1; break;
             case "Button2": channelNumber = 2; break;
             case "Button3": channelNumber = 3; break;
             default: break;
         }
         return true;
     }
     else
         return false;
 }

 void ChangeChannel()
 {
     DisableAllScreens();
     switch (channelNumber)
     {
         case 1: screen1.SetActive(true); break;
         case 2: screen2.SetActive(true); break;
         case 3: screen3.SetActive(true); break;
         default: break;
     }
 }

 void DisableAllScreens()
 {
     screen1.SetActive(false);
     screen2.SetActive(false);
     screen3.SetActive(false);
 }


000577084.jpg (244.7 kB)
Comment
Add comment · Show 4
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 instruct9r · Feb 03, 2016 at 09:50 PM 1
Share

So the script works, but if you include:

&& _hit.collider == thisCollider

The code inside condition stops executing, is that correct?

If so why don't you check what is the raycast hitting? If it's not thisCollider, then it shoud be something else, or if it hits something at all...

Is this script attached to the object, that have to be hit by the raycast?

avatar image AmazingDeveloper2 instruct9r · Feb 03, 2016 at 10:19 PM 0
Share

Without && _hit.collider == thisCollider I can press on any collider on the scene and it will work. But I want it to work only when I press the button. Yes, script attached to the object, that have to be hit by the raycast - empty GO with collider

alt text

423.jpg (91.9 kB)
avatar image instruct9r AmazingDeveloper2 · Feb 04, 2016 at 01:30 AM 1
Share

ok. Then the Raycast is hitting something.

Have you tried to Debug the hit.collider.name, to see what does it hit?

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

85 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

Related Questions

Unity 5 - My raycast is hitting my player, and I can't figure out why 2 Answers

How to make a "raycast" vertically in front of the player ? 0 Answers

Mobile Optimization question 1 Answer

How to Destroy game object horizontally and vertically , when hit by a Raycast 0 Answers

How to limit this particular raycast distance? 3 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