Search Unity

Bug Mesh collider error with Line renderer

Discussion in 'Physics' started by Khelgar, Jul 1, 2020.

  1. Khelgar

    Khelgar

    Joined:
    Mar 13, 2019
    Posts:
    16
    Hello,

    I have a file with a lot of coordinates. In my project I read this file and connect some points with a line renderer.
    I generate a mesh collider on all my line renderers because I need to do a raycast with them. When I want to pass the generated mesh to the mesh collider component I can have this error :

    Code (CSharp):
    1. [Physics.PhysX] TriangleMesh::loadFromDesc: desc.isValid() failed!
    2. UnityEngine.MeshCollider:set_sharedMesh(Mesh)
    I generated 1520 lines and I got this error twice.
    With a light file I havn't any error.
    This is my code, maybe it can help :

    Code (CSharp):
    1.     Mesh lineMesh = new Mesh();
    2.     MeshCollider lineCollider = line.AddComponent<MeshCollider>();
    3.     lineMesh.Clear();
    4.     lineRenderer.BakeMesh(lineMesh, Camera.main, false);
    5.     lineMesh.Optimize();
    6.     lineMesh.RecalculateNormals();
    7.     lineCollider.sharedMesh = lineMesh;
    Thanks you to help me to fix that ! :)
     
  2. Khelgar

    Khelgar

    Joined:
    Mar 13, 2019
    Posts:
    16
    When I change the boolean to true in the BakeMesh method, I have a lot of other errors.

    Code (CSharp):
    1. lineRenderer.BakeMesh(lineMesh, Camera.main, true);
    EDIT : The problems come from the bounding box. His generation is weird or I don't understand something. This is an example of my data and errors.



    My bounding box min and max don't always match with my lineRenderer points.
    Also I had this line after the BakeMesh method.
    Code (CSharp):
    1. lineMesh.RecalculateBounds();
     
    Last edited: Jul 6, 2020
  3. Khelgar

    Khelgar

    Joined:
    Mar 13, 2019
    Posts:
    16
    I solved it ! (But I didn't found the tool to put the thread on Resolved :()

    My problem was when I get the point positions of my line, sometimes the start and the end was the same point, so I tryed to draw a point and add a mesh collider on it.

    I fixed it with this way :

    Code (CSharp):
    1. Mesh lineMesh = new Mesh();
    2. MeshCollider lineCollider = line.AddComponent<MeshCollider>();
    3. lineMesh.Clear();
    4. lineRenderer.Simplify(0.1f);
    5. lineRenderer.BakeMesh(lineMesh, Camera.main, false);
    6. lineMesh.Optimize();
    7. lineMesh.RecalculateNormals();
    8. lineMesh.RecalculateBounds();
    9. Vector3 boundsSize = lineMesh.bounds.size;
    10. int count = 0;
    11. if (boundsSize.x == 0)
    12.     count++;
    13. if (boundsSize.y == 0)
    14.     count++;
    15. if (boundsSize.z == 0)
    16.     count++;
    17. if (count < 1)
    18. {
    19.     lineCollider.sharedMesh = lineMesh;
    20. }
    21. else
    22. {
    23.      DestroyImmediate(line); //line is my gameobject
    24. }
     
  4. zacharyaghaizu

    zacharyaghaizu

    Joined:
    Aug 14, 2020
    Posts:
    65
    Hi, With my project, I'm just getting a rectangle spawned instead of a mesh collider that follows the line. Do you know why this is happening?
     
  5. The_Pied_Shadow

    The_Pied_Shadow

    Joined:
    Jul 25, 2018
    Posts:
    14
    Check to see if mesh collider is set to be convex (first parameter in inspector). If it's set 'convex' then that could be your issue. Try setting that to false.