Search Unity

Displaying an objects MeshCollider

Discussion in 'Scripting' started by wxxhrt, Dec 21, 2014.

  1. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    I have a deformed cube with a non-convex hull. I want to convert the deformed cube so its vertices lie in the same places but it has a convex hull.

    I have MeshCollider attached to the deformed cube with Convex set to true and was hoping I could swap out the deformed cube's Mesh with the mesh the MeshCollider uses for collisions- but Im having no luck.

    I was hoping the MeshCollider's Mesh was stored in the undocumented MeshCollider.mesh and so tried this:

    Code (JavaScript):
    1.  
    2. var deformedMesh : Mesh = GetComponent(MeshFilter).mesh;
    3. var collider : MeshCollider = GetComponent(MeshCollider);
    4. var colliderMesh : Mesh = GetComponent(MeshCollider).mesh;
    5.  
    6. collider.convex = true; //set the MeshCollider up
    7. collider.sharedMesh = null; //the deformed mesh gets re-deformed every now and then
    8. collider.sharedMesh = deformedMesh;
    9.  
    10. deformedMesh = colliderMesh; //replace the cubes mesh with a convex version of itself?
    11.  
    12. //alsotried
    13.  
    14. deformedMesh.vertices = colliderMesh.vertices;
    15. deformedMesh.triangles = colliderMesh.triangles;
    16. deformedMesh.normals = colliderMesh.normals;
    But it seems that the MeshColliders Mesh is not stored in MeshCollider.mesh as the above code does nothing- does anyone know of a way of accessing the mesh the MeshCollider uses for collisions?

    Thanks
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The mesh is referenced in the (documented) MeshCollider.sharedMesh. MeshCollider.mesh is the instance; works the same way as Renderer.material and Renderer.sharedMaterial. But assigning colliderMesh to deformedMesh will not make a copy, if that's what you're trying to do; that's just a reference, which makes deformedMesh and colliderMesh point to the same thing.

    --Eric
     
  3. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163