- Home /
 
Is it possible to make a Mesh Collider thicker?
I'm having problem of moving rigidbody objects going through mesh terrain, even with '`Continous Collision Detection`' is enabled. A good solution proposed here is to make the ground collider thicker.
Now, since I am using a mesh collider for the ground, is there a way to make the mesh collider thicker?
Answer by WilliamLeu · Jul 23, 2014 at 01:01 AM
One way is to make a new mesh using the old mesh. Here's an example that uses the normal for each vertex, although it's far from perfect - mostly because the higher the perturbation, the smoother the mesh will get, and it doesn't handle seams to smoothing groups well. But, it's not a bad starting point.
 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 
     void Awake()
     {
         MeshCollider mc = GetComponent<MeshCollider>();
         mc.sharedMesh = ThickerMeshUsingNormals( mc.sharedMesh, 1.0f );
     }
     // Function only cares about verts, tris and normals...
     // Which should be fine because we're only messing with
     // this stuff for collision
     Mesh ThickerMeshUsingNormals( Mesh m, float fPerturb )
     {
         // USE THE NORMALS TO INFLATE THE MESH
         // We're going to store the new results as a new
         // verts list.
         Vector3 [] rv3OrigVerts = m.vertices;
         Vector3 [] rv3Norms = m.normals;
 
         int nVertCt = rv3OrigVerts.Length;
         Vector3 [] rv3NewVerts = new Vector3[ nVertCt ];
         for( int i = 0; i < nVertCt; ++i )
             rv3NewVerts[i] = rv3OrigVerts[i] + rv3Norms[i] * fPerturb;
 
         // CREATE AND RETURN THE MESH
         Mesh mRet = new Mesh();
         mRet.vertices = rv3NewVerts;
         mRet.triangles = m.triangles;
         // A cheap thing to do would be to just transfer the
         // old normals, although that wouldn't be right (both
         // in spirit, and mathematically).
         //
         // mRet.normals = rv3Norms;
         mRet.RecalculateNormals();
 
         return mRet;
     }
 
 }
 
              $$anonymous$$aking a mesh collider by using vertex's normal? That is smart. But I got your point. The higher the perturbation, the smoother it gets. I suppose it is like inflating a balloon.
Accepted your answer, since you provided an approach on how to make mesh collider thicker (Inflating). A good starting point.
Answer by markedagain · Jul 23, 2014 at 12:26 AM
how fast is the object moving to the ground ? and have u changed or altered game time in anyway ? is it anywhere on the ground or some areas where there is deformation ? also have u tried DontGoThroughThings
@markedagain How fast the objects move? Free fall by the gravity. Have I changed the game time? Nope. $$anonymous$$y terrain has a lot of deformation, and occasionally falls through it.
I tried that script at skin 1.0 and objects still penetrates, however, at lesser frequency than before.
if the issue happens in certain areas can u not simply put some small box colliers near ground level to fortify that gap ? not really a solution , and i dont have much more since i work very little with mesh colliders
Your answer
 
             Follow this Question
Related Questions
THIN objects can fall through the ground? (AMAZING SOLUTION) 2 Answers
My object falls through terrain. 8 Answers
Rigidbody object falling through floor. 8 Answers
Character with box collider sticks to walls 0 Answers
Throwing knife doesn't throw correctly 0 Answers