Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Issues with runtime generated MeshCollider

Discussion in 'Physics' started by OldHarryMTX, Mar 14, 2020.

  1. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Hi everyone!
    I'm recently facing some issues with a MeshCollider generated at runtime and used as a trigger.

    As you can see in the picture, i want to use a mesh as a Field of View. It is generated using a method similar to the one showed in this video, with a few more shrewdness.

    upload_2020-3-14_12-1-32.png

    WHAT I WANT: The red cube is detected OnTriggerEnter() and OnTriggerExit() only when entering or exiting the purple mesh.

    WHAT HAPPENS: The red cube is detected even in the "empty slices", as if the collider is completely circular.

    I've also tried using Physics.ComputePenetration(), but with the same results.

    Do you know why it acts that way, and if there's a way to solve this?

    I can use other methods to reach the result i want, but if i can i want to try to solve this one.

    Thankyou in advance.
     
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    Can you pause the game and check the generated meshCollider component in the scene view? Does it look like you’d expect it to look? Most importantly, is convex unchecked in the component? Otherwise the “empty slices” don't actually exist.
     
  3. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Yes, the mesh collider seems generated correctly, but the convex option must be checked because without that I can't check the trigger option.
    How can I use it as a trigger without the convex option checked?

    Thanks for your help
     
  4. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    Just to clarify: Since you had to check the "Convex" option, the actual shape of the collider is the convex hull of whatever shape you actually gave it. Think of a rubber balloon filled with air and your mesh inside. When you slowly release the air from the balloon, the skin of the ballon will not go into the cracks or convex parts of the mesh.

    The component tries to show this with the green wireframe. Probably that's also what you see in your case.
    Bildschirmfoto 2020-03-15 um 08.03.05.png

    As far as triggers are concerned, I think you're out of luck without further digging into the problem. I guess since you're creating the mesh yourself and using the fact that it's mostly pie-shaped, you could throw together a decomposition algorithm and create convex meshes.
     
    OldHarryMTX likes this.
  5. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    I'm afraid you are right. I think I will use another sistem to scan the area around the player, maybe still using that mesh to generate the fog of war...

    Thank you anyway for your help.
     
  6. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    You're welcome :)
    Since you're probably already doing a whole bunch of raycasts and in case you can also make assumptions about the minimum size of the objects you'd like to detect, you can maybe just skip the mesh overlap and just use the results of the raycasts.

    What might also be possible - if that's sufficient for your use case - is to switch to 2D physics. There you can have actual polygon colliders that probably do the decomposition based on triangles themselves. You could even mix 2d and 3d physics. 2D for overlapping tests and 3D in case you'd like to have objects fly around after explosions and such.
     
  7. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Yes, that's exactly what i'm done right now ^^

    Interesting, I'll keep it in mind.