- Home /
Updating Mesh Collider causes lag
I created a plane that the player can modify, but updating the mesh collider causes a significant drop in frames. I use this code to update collider:
hitTransform.GetComponent<MeshCollider>().sharedMesh = hitTransform.GetComponent<MeshFilter>().sharedMesh;
Is there any way to optimize it?
A permanent drop in frames? Or a drop while that code executes (guessing less than a second)?
No, only when executing the code - the one above.
Answer by Arano · Sep 19, 2019 at 02:49 PM
you could save a reference to the mesh/collider components, could help but i don't think it this can be the problem in your case.
MeshCollider collider;
MeshFilter filter;
void start() {
collider = hitTransform.Getcomponent<MeshCollider>();
filter = hitTransform.Getcomponent<MeshFilter>();
}
// your line of code:
collider.sharedMesh = filter.sharedMesh;
this is the only thing i can see right away you could improve, not sure if changing a meshCollider has any special effects on framerate.. did u try use the profiler to figure out what might cause this?
Ive done pooling tests with colliders before and other than wheel colliders exponential performance hits were taken when removing a group of 2d colliders or adding a group of 'normal' colliders
u might want to disable the collider before you make changes and enable it again when you are done (using a temporary copy maybe?)
The frame drops are clearly visible when the plane has more than 10,000 vertices. This is how the profiler looks like: And yes, I am sure that modifying $$anonymous$$esh Collider causes lag because by disabling collision refresh in the controller from terrafor$$anonymous$$g no frame drops occur.
Answer by hexagonius · Sep 19, 2019 at 02:59 PM
What really helps is the brand new Unity 2019.3 bake offloading capability: https://docs.unity3d.com/2019.3/Documentation/ScriptReference/Physics.BakeMesh.html
Sounds good, but the question is: Will I not have to bake it after every change in the mesh? If so, I doubt it would help.