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 /
avatar image
0
Question by pandatree12 · May 16, 2019 at 09:39 PM · raycastraycastingraycasthitraycasts

Raycast not working

     void Update()
     {
         Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             print(hit.transform.name);
             if (hit.distance <= 20 && hit.transform.CompareTag("Interactable"))
             {
                 text.text = hit.transform.GetComponent<Interactable>().GetDescription();
                 actionGfx.SetActive(true);
                 print("hit");
             }
             else
             {
                 actionGfx.SetActive(false);
                 text.text = "";
             }
         }
     }

Code looks fine to me, but for some reason it is creating some issues. The Raycast is fine, and can hit objects and retrieve their names. Line 7 runs, and prints the correct name of the object. However, for some reason nothing else runs inside the if condition. I even replaced the "hit.distance <= 20 && hit.transform.CompareTag("Interactable")" with "5 < 7" and the results are the same. Does anyone have any idea why everything is being ignored? Comment/reply for any questions regarding this, thanks.

Comment
Add comment · Show 1
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 Reedex · May 17, 2019 at 08:41 PM 0
Share

iam sure if you remove the distance check entirely, it works right?

2 Replies

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

Answer by pandatree12 · May 17, 2019 at 08:54 PM

Extracting the body of the Physics.Raycast conditon to a seperate method fixed the issue. Here's what it looks like now:

     // Update is called once per frame
     void Update()
     {
         Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             CheckRayValues(hit);
         }
         else
         {
             point.SetActive(false);
             description.text = "";
         }
     }
 
     // Raycast function
     private void CheckRayValues(RaycastHit hit)
     {
         if (hit.distance <= 1.5 && hit.transform.CompareTag("Interactable"))
         {
             point.SetActive(true);
             description.text = hit.transform.GetComponent<Interactable>().GetDescription();
             print(hit.distance);
         }
         else
         {
             point.SetActive(false);
             description.text = "";
         }
     }

Not sure what the difference between extracting the method and not makes, but I guess it was needed here.

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
0

Answer by JonnyHilly · May 17, 2019 at 05:02 AM

also print the distance on the same line as print(hot.transform.name) and print hit.transform.tag and you'll have a lot more information to figure out what is going wrong. Even easier... use the debugger and put a breakpoint on the print(hit.transform... line, and then just look at all the variables in the debugger and you'll see what it hit, how far, etc..... You can also step through one line at a time, to see if it goes inside the If... or not Check your tag name matches a real tag also (check the case sensitivity)

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 pandatree12 · May 17, 2019 at 08:27 PM 0
Share

I checked and the tag of the object matches the one in the if condition exactly and I checked everything else too. I will still try what you said and let you know if I can figure anything out. Thanks

avatar image pandatree12 · May 17, 2019 at 08:56 PM 0
Share

I was able to fix the issue buy just extracting the body of the Physics.Raycast condition to a separate method. Not sure why that was the case.

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

159 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 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

Raycast returns null for no apparent reason 0 Answers

[help]RayCast check [SOLVED] 2 Answers

ScreenPointToRay ray not coming through from camera 0 Answers

Can you figure out raycast origin position from RacyastHit? 2 Answers

My raycast is ignoring my tilemap collider 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