• 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
2
Question by Benjames · May 29, 2015 at 07:44 PM · raycastruntimeindextriangle

triangleindex isn't returning the correct triangle everytime

When I make a mesh at runtime, assign a mesh collider to i,t and try to get a triangle from a raycast it doesn't always return the correct triangle.

Is it a known fact that getting the triangle index is inaccurate? Does something get screwy when assigning a MeshCollider at runtime? Am I not making my mesh in a meshcollider cohesive way?

Here is a code snippet

         if(Input.GetKeyDown(KeyCode.J))
         {
             RaycastHit hit=new RaycastHit();
             Vector3 fr=Camera.main.transform.position;
             Vector3 f=Camera.main.transform.position+(Camera.main.transform.forward*300);
             Debug.DrawLine(fr,f,Color.blue,1);
             if(Physics.Raycast(new Ray(fr,(f-fr).normalized),out hit))
             {
                 
                 trees[0].triangles.Clear();
                 trees[0].triangles.AddRange(trees[0].mesh.triangles);
                 print("tri"+hit.triangleIndex*3);
                 trees[0].triangles[hit.triangleIndex*3]=0;
                 trees[0].triangles[hit.triangleIndex*3+1]=0;
                 trees[0].triangles[hit.triangleIndex*3+2]=0;
                 
                 trees[0].mesh.triangles=trees[0].triangles.ToArray();
             }
         }



Comment
Add comment · Show 7
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 Benjames · May 30, 2015 at 02:19 AM 0
Share

In the unity scripting api they have example code for triangle index that outlines the triangle with debug lines.

http://docs.unity3d.com/ScriptReference/RaycastHit-triangleIndex.html

I am going to do a test tonight that will closely mimic this code, to sort of cut out the middle man of the List that holds my triangles. I have a feeling that something in using a List is messing with my results.

If it doesn't work as intended I'm going to test it on an imported object not made a runtime. If it then works then I'll know that something is going wrong while making my mesh and setting up its collider.

avatar image maccabbe · May 30, 2015 at 02:50 AM 0
Share

Are you sure that the ray is hitting the collider of trees[0]?

avatar image Benjames · May 30, 2015 at 04:37 AM 0
Share

There is only one object in the scene. I'm at work other wise I would post the script that sets the mesh of trees[0] to the meshcollider. trees is just an array of a struct not an object, so trees also has a Gameobject variable "go".

avatar image Benjames · May 30, 2015 at 06:13 PM 0
Share

So I tested triangleindex with the mesh returned from the raycast and injected none of my own variables, still it seems inaccurate.

         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.J))
         {
             RaycastHit hit=new RaycastHit();
             Vector3 fr=Camera.main.transform.position;
             Vector3 f=Camera.main.transform.position+(Camera.main.transform.forward*300);
             Debug.DrawLine(fr,f,Color.blue,1);
             if(Physics.Raycast(new Ray(fr,(f-fr).normalized),out hit))
             {
                 $$anonymous$$esh mesh=hit.collider.gameObject.GetComponent<$$anonymous$$eshFilter>().mesh;
                 Vector3 p1=mesh.vertices[mesh.triangles[hit.triangleIndex*3]];
                 Vector3 p2=mesh.vertices[mesh.triangles[hit.triangleIndex*3+1]];
                 Vector3 p3=mesh.vertices[mesh.triangles[hit.triangleIndex*3+2]];
                 Debug.DrawLine(p1,p2,Color.red,9);
                 Debug.DrawLine(p2,p3,Color.red,9);
                 Debug.DrawLine(p3,p1,Color.red,9);
             }
         }


you can see in this picture the results

alt text

At one point the raycast line and the triangle outline where on completely seperate parts of the tree.

screen.png (336.7 kB)
avatar image Benjames · May 30, 2015 at 06:22 PM 0
Share

I changed $$anonymous$$esh mesh=hit.collider.gameObject.GetComponent().mesh; to $$anonymous$$esh mesh=hit.collider.gameObject.GetComponent().shared$$anonymous$$esh; And still got innacurate results.

So I imported an object and setup the mesh collider in the editor and tested that model. The results were 100% accurate. I'm thinking that my mesh isn't being setup in a way that is cohesive with the meshcollider math. ID$$anonymous$$ any help or speculation is appreciated.

alt text

screen.png (127.2 kB)
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Benjames · May 31, 2015 at 04:04 AM

OHHH snap son!!

So in part of my code I had it make a set of three ints for a triangle that really didn't make up a triangle...

so usually triangle arrays go 1,2,3, 2,3,4 ... something like that I had a set of three like 1,1,3 .. so didn't really make a triangle and threw the mesh collider off.

Anyways I removed that and RaycastHit.triangleIndex returns the correct triangle.

I hope this helps someone out.

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 Bunny83 · May 30, 2015 at 10:20 PM

Well the problem is most likely that unity doesn't really support updating the collision mesh at runtime. The workaround is to destroy the meshcollider and re-add the component. It should automatically pick up the mesh from the mesh filter.

This is a quite old and well known bug / limitation:

http://answers.unity3d.com/questions/945752/cannot-update-meshcollider-mesh.html

http://forum.unity3d.com/threads/how-to-update-a-mesh-collider.32467/

Comment
Add comment · Show 1 · 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 Benjames · May 30, 2015 at 10:42 PM 0
Share

Thanks for the response. I will keep picking at it a little longer and post if I ever get a good result.

It's upsetting because RaycastHit.traingleindex would be really useful for making procedural mesh's.

[EDIT]

I changed some code up to make some triangles be added to the array in order vs not in order, and it made the RaycastHit.triangleindex return the correct triangle. It's going to be really tough to get the rest of my code to add the triangles in order, when I do I'll post my results.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Export objects to a .3DS file at runtime 1 Answer

Odd Duplicate when selecting Triangle from Mesh (with Barycentric Selection.) 1 Answer

Split single triangle mesh into 2 Equal Parts 0 Answers

For raycast hits, what is a "barycentric coordinate"? 1 Answer

Unity re-ordering triangle indexes?? 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