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 ArqueroNET · Dec 09, 2017 at 12:01 PM · raycastraycastingraycasthit

Weird Raycast behaviour

NOTE: after reading the question read the comments, lots of more info there.

I have a sphere whit collider over a plane, and with rigidbody attached, but not forces in use at this testing moment. There are also a cube with a static collider, the one blue, and some others cubes without colliders, the little greens. I cast one raycast from each of the greens towards the sphere, and some ones dont collide. In the picture the fails are the gray lines, the cyan is when the ray hit any collider.

Nothing is moving nor rotating. A full static scene.

alt text

alt text

Code that cast rays:

     public bool BuscaEnlace(GameObject goA, GameObject goB, out Vector3 enlace)
     {
         enlace = new Vector3(0, 0, 0);
         Vector3 puntoA = goA.transform.position;
         Vector3 puntoB = goB.transform.position;
         RaycastHit hit;
         foreach (Vector3 espejo in noVisuales)
         {
             if (Physics.Raycast(espejo, puntoA - espejo, out hit))
             {
                 Debug.DrawLine(espejo, hit.point, Color.cyan, Mathf.Infinity);
                 Debug.Log("Vista a " + goA.name);
                 if (hit.transform.gameObject.name == goA.name)
                 {
                     if (Physics.Raycast(espejo, puntoB - espejo, out hit))
                     {
                         if (hit.transform.gameObject.name == goB.name)
                         {
                             Debug.DrawLine(espejo, hit.point, Color.gray, Mathf.Infinity);
                             Debug.Log("Vista a " + goB.name);
                             enlace = espejo;
                             return true;
                         }
                     }
                 }
             }
             else
             {
                 Debug.DrawRay(espejo, puntoA - espejo, Color.gray, Mathf.Infinity);
             }
         }
         return false;
     }

It first look for linear sight of the sphere and then for the same to the cube in the right, something like a mirror, but fails and i dont know why.

If i change the code to this:

     public bool BuscaEnlace(GameObject goA, GameObject goB, out Vector3 enlace)
     {
         enlace = new Vector3(0, 0, 0);
         Vector3 puntoA = goA.transform.position;
         Vector3 puntoB = goB.transform.position;
         RaycastHit hit;
         foreach (Vector3 espejo in noVisuales)
         {
             if (Physics.Raycast(espejo, puntoA - espejo, out hit))
             {
                 // Debug.DrawLine(espejo, hit.point, Color.cyan, Mathf.Infinity);
                 // Debug.Log("Vista a " + goA.name);
                 // if (hit.transform.gameObject.name == goA.name)
                 // {
                 //     if (Physics.Raycast(espejo, puntoB - espejo, out hit))
                 //     {
                 //         if (hit.transform.gameObject.name == goB.name)
                 //         {
                 //             Debug.DrawLine(espejo, hit.point, Color.gray, Mathf.Infinity);
                 //             Debug.Log("Vista a " + goB.name);
                 //             enlace = espejo;
                 //             return true;
                 //         }
                 //     }
                 // }
             }
             else
             {
                 Debug.DrawRay(espejo, puntoA - espejo, Color.gray, Mathf.Infinity);
             }
         }
         return false;
     }

I get only the gray lines as result. So i think not is the code, is almost commented, but i always read that raycast dont fail...

¿Some help?

help2.png (175.3 kB)
help1.png (167.8 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 ArqueroNET · Dec 09, 2017 at 01:11 PM 0
Share

New clue, If I remove rigidbody raycast works well...

With rigidbody some rays works some others no, as seen in the previous images...

No forces applied. Only component added.

Tested variations, kinematic, gravity on/of, freeze x/y/z and completly freezed, none works, if rigidbody present some rays fail as seen before.

Without rigidbody attached image: alt text

Now if I attach rigidbody AND ADD a "background" static collider as seen in image, also works... ¿why?

alt text

Without that new static collider some rays dont work, but the debug rays go to the center of sphere...

So if a remove rigidbody or add far collider detects sphere, but without that dont... can someone enlighten me, please?

help5.png (137.9 kB)
help4.png (121.2 kB)
avatar image ArqueroNET · Dec 09, 2017 at 01:45 PM 0
Share

New images with rigidbody attached (no forces applied), moving sphere before run. Gray lines are raycast returning false.

alt text

alt text

help6.png (172.8 kB)
help7.png (152.8 kB)
avatar image ArqueroNET · Dec 09, 2017 at 02:09 PM 0
Share

If i keep calling the function, eventually i get the expected behaviour with rigidbody and without far border collider, but... ¿why fails the first time always on some raycasts?

avatar image ArqueroNET · Dec 09, 2017 at 02:46 PM 0
Share

Test projectlink text

Run out of box and check fail in raycast.

  • If enable "BorderTest1", run successfully

  • If you remove rigidbody from "Test1" sphere, run successfully.

  • If change:

       void Deambula()
         {
             ObtenObjetivoDebug();
             ObtenRuta();
             //InvokeRepeating("ObtenRuta", 0, 1);
         }
    
    

to:

     void Deambula()
     {
         ObtenObjetivoDebug();
         //ObtenRuta();
         InvokeRepeating("ObtenRuta", 0, 1);
     }

First fail, in one second success.

Now can disable test1 (and bordertest1) and do the same tests with test2 and bordertest2, in another start point.

Please help to understand that behaviour, why need borders?, why if i dont add border at the second call (InvokeRepeating) without changes in the scene success? what does the rigidbody have to do?

weirdcast.zip (223.5 kB)

0 Replies

· Add your reply
  • Sort: 

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

102 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

Related Questions

Is this way of making a raycast correct 2 Answers

Raycast returns null for no apparent reason 0 Answers

Raycast not hitting instantiated object when timescale is 0 1 Answer

Rigidbody.position causes shaking 1 Answer

RayCast can't detect GameObject at Player's feet 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