Mesh.Bake.PhysX.CollisionData Profiler Spike Causes
Unity profiler spikes from Mesh.Bake.PhysX.CollisionData means the mesh for a collision is being baked at runtime! Instead collisions data should be baked in the editor when entering play mode or when building your project.
If you’re seeing this Unity performance spike in your game at runtime then either:
- A collision event is being triggered against a mesh collider which is not scaled to 1,1,1 scale
- Prebake collision meshes is disabled in Player Settings > Other Settings > Prebake Collision Meshes
- One of the objects in the collision with a rigidbody is marked as static
How to fix Mesh.Bake.PhysX.CollisionData
There’s a few options for removing the expensive runtime calculation from your Unity project.
- The most basic solutions first. Make sure prebake collision meshes is enabled in Player Settings > Other Settings > Prebake Collision Meshes and that none of your objects with rigidbodies are marked as static!
- Replace the mesh collider(s) causing the spike with primitive colliders instead, such as a combination of box colliders and sphere colliders which create the shape of your mesh. This will allow you to continue scaling your object away from 1,1,1 without the performance spike of the collision data being recalculated for collisions.
You don’t always need high accuracy colliders so if you just need basic collisions then a few box colliders should be fine.
- Edit the model and manually change the scale OR select the model and change the model import scale. This will scale the raw model file rather than the model being used at runtime.
(Note: This option is not optimal if you still need to resize the object at runtime or need the object at different sizes in the scene)
- Reduce the amount of collisions which the scaled model is involved in! This may not always be the best solution, but if you really do need to keep the model as a mesh collider not at a 1,1,1 scale then just try to keep the amount of collision events the model will be involved in as little as possible.
If you already have a model which you didn’t expect to be in as many collisions as it was then try debugging it by attaching a script to the collider with OnCollisionEnter and OnCollisionStay functions and check the source of the collisions.