- Home /
Changing mesh collider at run time
Hi there, I'm having some issues with my mesh collider. During run time I'm changing the vertices and I want to update the mesh collider to match the visual changes of the mesh.
Visibly the mesh is changing, but the collision volume is remaining as it's original shape - I'm using mesh.RecalculateBounds() to update the collider after I assign the updated vertices back to the mesh (I believe that's the correct approach) but this doesn't seem to work.
Any suggestions? Thanks! Ant
Answer by HannesR · Apr 29, 2013 at 03:25 PM
You have to set the collider mesh to null and then assign the updated mesh.
the$$anonymous$$eshCollider.mesh = null;
the$$anonymous$$eshCollider.mesh = theUpdated$$anonymous$$esh;
Answer by keithkeith · Nov 25, 2014 at 12:00 AM
This doesn't seem to work anymore. Destroying and recreating the meshcollider does.
DestroyImmediate(this.GetComponent<MeshCollider>());
var collider = this.AddComponent<MeshCollider>();
collider.sharedMesh = myMesh;
Hello, I am having issue with this 2 years later, and none of these options work anymore, after my mesh changes, i want to update the mesh collider like this, and it doesnt work in unity5 anymore i guess... pls halp
mesh.vertices = vertices;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
DestroyImmediate(this.GetComponent<$$anonymous$$eshCollider>());
$$anonymous$$eshCollider collider = gameObject.AddComponent<$$anonymous$$eshCollider>();
collider.shared$$anonymous$$esh = mesh;
Answer by lil_sichen · Dec 14, 2017 at 08:16 AM
I have tried all solutions out in Unity 2017.2.0f3 but none worked. Here is my observation and solution: If you assign a "used" mesh like this:
var newMesh = ProceduralGen.GetNewMesh();
DestroyImmediate(this.GetComponent<MeshCollider>());
var collider = gameObject.AddComponent<MeshCollider>();
collider.sharedMesh = newMesh;
it won't work for some reason. However, in my case, when I assigned the procedurally generated Mesh straight to the collider's sharedMesh, it worked:
var collider = gameObject.GetComponent<MeshCollider>();
collider.sharedMesh = ProceduralGen.GetNewMesh();
Also, in this case you don't have to worry about the order in which you assign the meshFilter and collider's meshes. Plus, there is no need to remove and add the collider! This is definitely not the best solution for those of you who have to deal with heavy, complicated procedural generation, but in my case it worked fine. Hope this helps!
Answer by vladibo · Jul 05, 2017 at 02:28 AM
var smr = _renderer;
var mc = _collider;
var colliderMesh = new Mesh();
smr.BakeMesh(colliderMesh);
mc.sharedMesh = null;
mc.sharedMesh = colliderMesh;
Answer by Cursedth · Oct 12, 2017 at 06:16 AM
Most practical solution here suggested by defaxer: http://answers.unity3d.com/questions/193630/modify-mesh-collider-when-modifying-vertices.html
Workd for me. Just get the
myGamObject.GetComponent<AnyCollider>().gameObject.SetActive(false then true again);
Your answer
Follow this Question
Related Questions
Why do objects pass through a mesh/ 2 Answers
Anyway to make my object not fall through the floor without convex? 0 Answers
Vertex Animation 1 Answer