Search Unity

how to add colliders on my probuilder mesh by code

Discussion in 'World Building' started by Raheel_unity572, Mar 11, 2020.

  1. Raheel_unity572

    Raheel_unity572

    Joined:
    Jun 21, 2019
    Posts:
    4
    Hello,

    I am using the probuilder mesh creation during runtime. e.g.

    // Create faces with the given back wall points for the drywall
    Face[] faces = new Face[] { new Face(CreateQuadFaces(backWallPoints)) };
    // Create Probuilder Mesh
    ProBuilderMesh myMesh = ProBuilderMesh.Create(backWallPoints, faces);
    // Set the material as defined in the Manager
    myMesh.SetMaterial(myMesh.faces, backWallMat);
    // Triangulate edge smoothing to have sharp edges
    myMesh.ToTriangles(myMesh.faces);
    // Refresh mesh content
    myMesh.Refresh();

    I am trying to figure out how to add colliders on this mesh that I created and I cannot find any documentation on this. Can someone help me out?

    Thanks
     
  2. antoinebr_unity

    antoinebr_unity

    Unity Technologies

    Joined:
    Nov 16, 2018
    Posts:
    23
    To get the result you want, you can add a MeshCollider component to the object before calling refresh. With your code example it should look something like this:

    Code (CSharp):
    1. Face[] faces = new Face[] { new Face(CreateQuadFaces(backWallPoints)) };
    2. ProBuilderMesh myMesh = ProBuilderMesh.Create(backWallPoints, faces);
    3.  
    4. //Add collider
    5. myMesh.gameObject.AddComponent<MeshCollider>();
    6.  
    7. myMesh.SetMaterial(myMesh.faces, backWallMat);
    8. myMesh.ToTriangles(myMesh.faces);
    9. myMesh.Refresh();
    Calling myMesh.Refresh() will recalculate all the mesh attribute, including the collider. If you only wan't to recalculate the collider, you can call myMesh.Refresh(RefreshMask.Collisions). For more info, here's the Refresh documentation.

    Hope this helps
     
  3. Raheel_unity572

    Raheel_unity572

    Joined:
    Jun 21, 2019
    Posts:
    4
    perfect. Thanks!
     
  4. MisterRogers

    MisterRogers

    Joined:
    Mar 15, 2015
    Posts:
    5
    Can the colliders be updates every frame with refresh?