• 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 MakakWasTaken · Apr 22, 2016 at 01:36 PM · raycastcollidermeshraycasthitlinecast

Physics.Raycast not hitting anything

For some reason my Physics.Raycast isn't hitting anything:

         Vector3 endPoint = Vector3.zero;
         for (int e = 0; e < mesh.vertices.Length; e++)
         {
             Vector3 StartPoint = mesh.normals[e] * (Radius + MaxHeight + 10.0f);
             RaycastHit hit;
             if (Physics.Raycast(endPoint, StartPoint, out hit))
             {
                 Debug.DrawLine(StartPoint, hit.point, Color.yellow);
                 Debug.DrawLine(hit.point, endPoint, Color.red);
                 colors[e] = new Color(0.5f, 0.5f, 0.5f, 1.0f);
             }
             else
             {
                 Debug.DrawLine(StartPoint, endPoint, Color.green, 5f);
             }
         }

I have tried with both a MeshCollider and a spherecollider and none of the rays turn either yellow or red, they remain green. Am I using it wrong?

EDIT: It seems that it doesn't hit because there isn't any collider, even though I have a meshcollider attached to it. I update my meshcollider like so:

         mesh.Clear();
         mesh.vertices = vert;
         mesh.normals = norm;
         mesh.triangles = tri;
         mesh.uv = uv;
 
         mesh.RecalculateBounds();
         mesh.RecalculateNormals();
 
         mc0.sharedMesh = null; // nullify the previous sharedMesh
         mc0.sharedMesh = mesh;
 
         mc0.enabled = false;
         mc0.enabled = true;
Comment
Add comment · Show 2
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 Cherno · Apr 22, 2016 at 01:49 PM 0
Share

ns$$anonymous$$d of using Deb rays, try inserting Debug.Log lines to write the collider, if any, that was hit to console.

avatar image MakakWasTaken Cherno · Apr 22, 2016 at 05:39 PM 0
Share

Okay, I found out that it hits 4/1536 points, which is nothing when the goal is for all of the points to hit.

EDIT: nvm, it was hitting a small capsule collider, which it wasn't supposed to hit.

1 Reply

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

Answer by MakakWasTaken · Apr 22, 2016 at 08:18 PM

I actually figured it out, thanks to this post: http://answers.unity3d.com/answers/1153623/view.html

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

63 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

Related Questions

RaycastHit not working for Mesh Collider 0 Answers

How to Instantiate a line or a ray that goes through all colliders and send a message to them???? 1 Answer

Raycast goes through collider 2 Answers

Enemy line of sight using linecast and colliders 1 Answer

Linecast from raycast hit point 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