Search Unity

Generating MeshCollider via scripting for LineRenderer

Discussion in 'Editor & General Support' started by jarileon, Mar 15, 2019 at 8:42 AM.

  1. jarileon

    jarileon

    Joined:
    Jun 2, 2017
    Posts:
    77
    I need to create a MeshCollider for my LineRenderer. I googled and saw that previously this has been quite complicated to achieve as the collider needed to be generated per vertex etc. However I was delighted to notice that LineRenderer has a BakeMesh() method for getting the mesh of the line.

    But when I try to create a MeshCollider for it using LineRenderer.BakeMesh(), I get error:

    "Failed extracting collision mesh because vertex at index 2 contains a non - finite value(nan, nan, nan).Mesh asset path "" Mesh name "" UnityEngine.MeshCollider:set_sharedMesh()"

    My code:

    Code (CSharp):
    1.  
    2. Mesh lineMesh = new Mesh();
    3. line.BakeMesh(lineMesh, myCamera);
    4.  
    5. MeshCollider meshCollider = line.gameObject.AddComponent<MeshCollider>();
    6. meshCollider.sharedMesh = lineMesh; //ERROR: Failed extracting collision mesh because vertex at index 2 contains a non - finite value(nan, nan, nan).Mesh asset path "" Mesh name "" UnityEngine.MeshCollider:set_sharedMesh()
    It seems that the vertices have really huge numbers when I look at lineMesh.vertices:



    Is this a bug in BakeMesh() or am I missing something? The docs do not really help, there is no code examples or anything..

    My line is working fine and is visible in the scene with not problems.