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.
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?
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.
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.