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
1
Question by JJaacckkyyee · Jan 06, 2020 at 01:02 AM · raycastlinerenderermeshcollider

How can I raycast a mesh collider baked with LineRenderer.BakeMesh() ? Ty

I have two classes, one for raycasting and one for baking the mesh, here they are:

 void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit, 100f))
             {
                 print(hit.transform.gameObject.name);
                 if (hit.transform != null)
                 {
                     print(hit.transform.gameObject.name);
                 }
             }
         }
     }

and second this:

     void Start () {
         LineRenderer lineRenderer = gameObject.GetComponent<LineRenderer>();
         MeshCollider meshCollider = gameObject.AddComponent<MeshCollider>();
 
         Mesh mesh = new Mesh();
         lineRenderer.BakeMesh(mesh, Camera.main, true);
         meshCollider.sharedMesh = mesh;
     }

At runtime the mesh does appear and I can confirm that it takes the shape that i want, the one of the line rendered, the problem is that while the raycast class works fine with all other colliders it isnt working with the one generated this way. A couple of users used this code to bake their mesh, the code as I said seems to work fine and is nearly identical to that shown on the documentation for meshCollider.sharedMesh, still they didnt appear to use it for raycasting. Some other user said it may be related to which side is the mesh facing but I'm pretty sure that's what the Camera.main is there for.

Hope you can help me understand the issue, thanks

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

1 Reply

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

Answer by lgarczyn · Jan 06, 2020 at 08:10 PM

Unfortunately, Raycast only works on concave mesh, but your line renderer is surely convex.

  • You could try to use multiple box colliders forming a chain along the line

  • If your line is flat, you could have a BoxCollider containing the entire line, then use the collision point to compare it to every quad, and check if it is inside.

  • If your line is flat, you could try to convert it to a 2d polygon collider, and use its API to check if the previous point falls inside it

Comment
Add comment · Show 3 · 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 JJaacckkyyee · Jan 11, 2020 at 12:44 PM 0
Share

Ty, for the explanation and the suggestions, I'll try them out :)

avatar image The_Pied_Shadow · Mar 31, 2021 at 04:07 PM 0
Share

I'm having the same issue but your answer seems to be opposite of what I'm experiencing. If I set the mesh collider to be convex the ray cast will hit it, but if it's not set to convex (making it concave) the ray cast will not hit it. But I can't set it to convex because then instead of a mesh following the line I just get a large rectangle which will not work for my purposes.

avatar image The_Pied_Shadow · Mar 31, 2021 at 05:42 PM 0
Share

I've deter$$anonymous$$ed that a concave mesh renderer will in fact received ray casts, however, the mesh baked by the line renderer has a 'front' and 'back'. If the ray hits it from behind it will pass through without registering. This is my issue. For some reason the line renderer is baking a mesh facing directly away from my camera (where the ray is co$$anonymous$$g from with a mouse click). I know because if I turn the object around then the ray cast hits.

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

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

Related Questions

RaycastHit Triangle Index Problem 0 Answers

Simple wire/cable 1 Answer

How to scan a surface of a GameObject and get the distance of every edge to camera 1 Answer

RayCast Line Not Rotating with Gun? 0 Answers

Fill area defined by a contour 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