How to update a mesh collider?

Hello,

I’m developing a video game student project.
A feature consists to move a bridge like Tacoma bridge

I move the mesh with bones, so the mesh moves correctly, but the mesh collider is not updated.
My case : http://www.vimeo.com/7163412

The bridges has a mesh collider and the cubes have got box colliders. The mesh collider of the bridge has got less than 255 faces.

Do you know how I could do this?

Hi - welcome to the forum!

If you are using a boned animation created in a 3D application, the MeshCollider will not be updated as the animation plays, it will just stay in the initial position. However, you can update the collider if you are animating the mesh from a script with the Mesh API - you just need to assign the mesh to the sharedMesh property of the MeshCollider each frame.

4 Likes

I tested to assign sharedMesh collider in Update:

function Update () 
{
	if(Input.GetButton ("Fire1"))
	{
		//init:
		//get the bone
		var bone2 = transform.Find("bone02"); 
	    //initial force
		var upForce = Time.deltaTime * 10;
		var rotForce = Time.deltaTime * 40;
		//input
		var inputH = Input.GetAxis("Horizontal2");
		var inputV = Input.GetAxis("Vertical1");
		
		//move the bone
		bone2.transform.Rotate(Vector3.right*inputH*rotForce);
		bone2.transform.Rotate(Vector3.forward*inputV*upForce);
	}
}

function LateUpdate () 
{
	//update the mesh collider
	var skinnedMeshRenderer = GetComponent (SkinnedMeshRenderer);
	collider.sharedMesh = skinnedMeshRenderer.sharedMesh;
}

But nothing better, mesh collier is alway initial mesh pattern. Did I a mistake?

1 Like

I’d be interested in the answer to this, as I don’t understand why mesh colliders can’t be updated. If I have a tank whose turret I want to rotate via a named bone, then it sounds like I need to manually deal with updating the mesh, or do away with it and use primitives instead.

Mesh colliders need serious pre-processing to function, and are not suitable for updating in real-time. You can’t get the updated mesh from a skinned mesh renderer for technical reasons that I forget exactly, but even if you could, it would still be very slow to compute the mesh collider. Something like a tank is trivial to do using primitive colliders, and that’s definitely the way to go.

–Eric

not sure if you still on this. there seems to be some change with unity 3.0 Previously
GetComponent().sharedMesh = meshFilter.mesh;

would work but in unity3 u need to null the mesh before assigning a new one.

GetComponent().sharedMesh = null;
GetComponent().sharedMesh = meshFilter.mesh;

8 Likes

did you resolve?

I couldn’t resolve!!
http://forum.unity3d.com/threads/110128-How-to-modify-a-collider-according-to-a-bone

pkamat, thank you! I was pulling my hair out (and I don’t have much left!)

1 Like

Thank you so much!! I’ve been working on this for a whole day!

Thanks for saving me a few hours of frustration!

Worked for me too!

thanks

Thanks you a lot. I lost a morning for that fix.

It’s solved it but it’s kind of weird though to null the mesh before re-assigning it. :face_with_spiral_eyes:

1 Like

WOOHOO thanks!!! great tip!

pkamat, thank you very much!

Yep, I can confirm that this does indeed force the mesh collider to update…

But I can also confirm that, in my case at least, the result really is too slow (as Eric5h5 predicted). I’ve got dynamic terrain sculpting, but I certainly can’t call this on every frame while the terrain changes.

Hmm, but maybe I don’t need to… I may be able to call it only on mouse-up, at least for this particular case. Or, failing that, I’ll have to walk the mesh myself, find the closest few vertices, and interpolate, but… ugh. That gets ugly fast.

I know you posted this years but I didn’t find anything else that answered my question and I hope you (or someone else who knows the answer) will still read this: If updating a MeshCollider is so costly, what alternatives and workarounds are there?

In my case I’m working on a relatively simple “toon style” ocean (think Zelda:Wind Waker). I think I won’t need very realistic and detailed wave forms so I’ll try to go with just some sinus vertex deformations (probably with some perlin noise added on top) that I’ll apply on a plane. But of course I’d also like to have the waves interacting with ships and such and I don’t see how I’d do this without constantly updating the MeshCollider.

Thank you very much for any pointers you can give me!

Edit: I assume if the water isn’t too stormy and sluggish enough it’d suffice to update the MeshCollider only every few frames but I’d still be quiet often i assume and hopefully there’s a better way…

I think your best bet is to only calculate wave height where/when you need it, instead of calculating it across the entire mesh. It’s frustrating that updating colliders is so slooooow, because it’s the obvious and easiest way to do it. So for example with a boat object you need to build a function that will calculate the wave height at the boat’s position, and then perform an offset to simulate buoyancy.

This is what I do in Suimono, and it works really well, but I still wish I could have just used the original collider. It would have saved me months of work and frustration. I know Unity 5 has an improved physics system but I haven’t tested if mesh collider updates work any faster.

Thanks heaps … I was soo stuck (still relevant with Unity 5)!

1 Like

A small syntax error with all my respect:

GetComponent().sharedMesh = null;
GetComponent().sharedMesh = GetComponent().mesh;

OMG THANKYOU!!!