Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Generating MeshCollider via scripting for LineRenderer

Discussion in 'Editor & General Support' started by jariwake, Mar 15, 2019.

  1. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    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.