- Home /
Modify Mesh Collider when modifying vertices?
I am modifying vertices on some objects in my scene, but if they have a Mesh Collider on them, that collider retains the original shape.
I think this is because it is still tied back to the original Mesh, which is in essence, a prefab for the mesh component.
Is there a way to make a unique collider for a modifed mesh?
Hey guys...
I´m having a problem which is quite the same. BUT the Problem about it, is that I have to update the collider shared $$anonymous$$esh every frame (at least 30 times per second)...
Do you have any idea how to get this effect ?! The vertices count is around 2000.
The Project is about: I´m moving my vertices of the terrain in Y-Space. $$anonymous$$y Objects have to be on top of the mesh so they won´t fall through the terrain. If the update is to slow they will fall down. :( You know what I mean ?!
I thought about using iTween but I´m not sure about the technique ob this... does it help me ?! So... does it use vertices to calculate the movement (this would solve everything!) or is it just using the meshcollider (if so ... my problem is still there)
thanks a lot !! :)
@Lka, @defaxer @Statement: None of the above solutions worked for me :-((
I have a rope hanging between two poles. The rope has physics properties and thus the $$anonymous$$eshCollider must be as dynamic as the rope. But, the $$anonymous$$eshCollider stays statically like a straight line between the poles. I have tried all solutions above and none of them worked!
Any help will be appreciated!
Answer by Bunny83 · Dec 09, 2011 at 10:02 PM
Shouldn't setting MeshCollider.sharedMesh to your new mesh be enough? ;)
This does the trick, too...
I wish I could have two answers, but this one does a slightly more direct job, so I have to give this one the extra +1.
Actually, I tried replacing my code with this approach. The problem is that the collision mesh won't update. I am reusing a mesh, not creating a new one. $$anonymous$$aybe the shared$$anonymous$$esh property doesn't rebuild the collision model if the same $$anonymous$$esh reference is being set (even if the contents of that mesh differ from the last time). @Tasarran, did you verify that this solved the issue on your end? Then I can't figure out what else I might be doing wrong. Just setting shared$$anonymous$$esh to the same mesh won't update collision for me :/
@Statement: Yep, that could be. I wish Unity would provide an "Update / Commit" function for such cases. At the first glance it's nice if something happens automatically, but it only reduces the possibilities or can get you in trouble since you can't specify when you want the update happen.
@Statement Yes, it works fine for me, but I am not changing the topology or number of points in my mesh, just moving them around.
I think since (it seems) you are punching holes and adding/removing points, you might be a special case...
Your way did work for me, too, but like I said, I had to give the nod to Bunny's answer for being one step less :)
Answer by Lka · Dec 09, 2012 at 12:58 AM
setting meshcollider=null and then meshcollider=mesh seems to work in unity 4
mc = go.GetComponent<MeshCollider>();
if(mc==null)
mc = (MeshCollider) go.AddComponent (typeof(MeshCollider));
else
{
mc.sharedMesh = null;
mc.sharedMesh = mesh;
}
Thanks, this is a cleaner method than destroying and remaking the $$anonymous$$eshCollider object. I'm assu$$anonymous$$g it has less overhead, too.
in mc.shared$$anonymous$$esh = mesh; What does mesh equal?
Answer by Statement · Dec 09, 2011 at 06:19 PM
Yes, but it's slow. I used to do this when I was using mesh colliders for my voxel terrain. If anyone else have a better answer, then I'll vote you up!
void Update()
{
if (chunk.Rebuild(mesh))
{
Debug.Log("Rebuilt mesh");
DestroyImmediate(collider);
gameObject.AddComponent<MeshCollider>();
}
}
Basically, you need to destroy the collider and add it back on again.
Thanks! I don't $$anonymous$$d if it's slow, I'm only doing it once, at startup. $$anonymous$$ight be a 'load lag', but that's a non-issue as far as game play goes.
Works perfectly, thanks again! Adding an up vote for the quick and useful advice.
Answer by defaxer · May 27, 2015 at 12:38 PM
Not really an elegant solution, but disabling and then enabling MeshCollider works fine for me
Compared to all the other options this still seems to be the best solution right now.
Answer by keithkeith · Nov 25, 2014 at 12:00 AM
Just nulling the mesh and resetting it doesn't seem to work anymore. I had to destroy the whole collider and recreate it.
DestroyImmediate(this.GetComponent<MeshCollider>());
var collider = this.AddComponent<MeshCollider>();
collider.sharedMesh = myMesh;