4
\$\begingroup\$
  • Do Mesh Colliders slow down performance simply by existing or only when they are colliding?
  • Do they directly impact FPS or do they slow down the in-game physics?
\$\endgroup\$
13
\$\begingroup\$

A moving body near a meshcollider needs to check for collisions against potentially every triangular face of the mesh. So for a detailed or non-convex mesh, or lots of bodies, this can get expensive. And of course, if you have a meshcollider moving near another meshcollider, you pay this cost combinatorically.

These costs hit during the physics update step, as part of its collision resolution. The physics engine doesn't know where to position things for the next frame until it's worked out which objects need to bounce or stop due to these collisions. So in extreme cases, this can make the physics update process take longer and delay rendering of the next frame, impacting the game's frames per second rate.

If your frame runs extremely slow, past the maximum allowed timestep configured in the Time Manager then Unity will start dropping physics steps to try to keep things moving. Only if that happens will your physics slow down relative to realtime, otherwise Unity's decoupled fixed timestep will keep your physics moving consistently even if the rendered FPS drops.

When the meshcollider is not moving, the physics engine can put it to sleep, and only check it when another body comes close enough to potentially hit it, saving quite a lot of computation.

So the cost isn't fixed every frame, it will vary based on the complexity of the physics interactions that need to be checked & resolved in that frame.

To manage this cost, your best bets are:

  • Favour simpler collider types for dynamic objects, or terrain colliders for environments
  • Minimize the number of faces in meshes used for collision
  • When possible, make mesh colliders convex, or break them into convex pieces

    (convex meshcolliders can use faster algorithms than an exhaustive face-by-face check, though the worst-case complexity still grows with the face count in the mesh)

  • Avoid moving big mesh colliders — see if you can keep them static and move things relative to them to create a sense of motion for big environmental collision shapes like a boat/spaceship
\$\endgroup\$
  • \$\begingroup\$ How do these costs show? Do they directly impact FPS? \$\endgroup\$ – AzulShiva Aug 27 '17 at 16:28
  • \$\begingroup\$ TIL a new word: combinatorically -- of, relating to, or involving combinations \$\endgroup\$ – Bradley Flood Aug 28 '17 at 1:45
4
\$\begingroup\$

A Mesh Collider will have to figure out the collisions for every single face of the whole mesh, Therefore It would take Longer (No significant difference most of the time) and would consume more Hardware Resources which would also slow down the performance.

Q -) Do they reduce performance simply by existing or Colliding ?

A -) No they don't reduce performance by just existing, The performance gets a hit when they are colliding.

Q -) Do they directly impact FPS or do they slow down the in-game physics?

A -) Essentially in a Mesh Collider. The more the triangles, The more the performance hit and So on. Most developers actually spend too much time trying to optimize uncritical parts of their projects and This sometimes leads to the poor optimization of the actual critical parts. However to only answer Your question, Yes mesh colliders do actually Impact Fps if they have a lot of faces.

\$\endgroup\$
  • \$\begingroup\$ "Most developers actually spend too much time trying to optimize uncritical parts of their projects" I want bullet holes and other effects on the surfaces of all objects. Are Mesh Colliders for ALL game objects (max 150k tris) acceptable for this purpose? (I will use simple capsule colliders on all moving entities for bumping into objects and a series of box colliders for hit detection only) \$\endgroup\$ – AzulShiva Aug 29 '17 at 6:35
  • 1
    \$\begingroup\$ No. Mesh Colliders for ALL Game objects are not acceptable for this purpose, Only the most complex ones. If you want to have 100% correctly placed bullet holes, You can use Mesh Colliders but I am sure there are better options. I am not sure but can't you simply use a raycast and see where It hit on the model without using a physics collider ? You may want to look into this thread: forum.unity3d.com/threads/14378-Raycast-without-colliders \$\endgroup\$ – Demoncious Aug 29 '17 at 13:38
  • \$\begingroup\$ I could make all the colliders ignore the player. So the only thing "colliding" with them would be my raycasts. I'd throw a simple cube in for player collisions. Would that work or would the stationary objects's Mesh colliders interfere with each other / attempt to detect each other's collidion? (Rigidbody and any Physics are disabled, it's just random motionless stuff laying around) \$\endgroup\$ – AzulShiva Sep 2 '17 at 20:19
  • \$\begingroup\$ Experimenting is the best thing that can increase your knowledge. It seems like It would work but Just as I said I've not tried it. You should try it out and post the results here for anyone that may face this problem in the future. \$\endgroup\$ – Demoncious Sep 2 '17 at 20:35
  • \$\begingroup\$ Honestly, I don't know much about unity. Disabling all MeshColliders doesn't yield any noticeable fps increase. Bulleteffects and muzzleflashes reduce fps from 250 to 150. I will be focusing on the latter... \$\endgroup\$ – AzulShiva Sep 3 '17 at 12:44
3
\$\begingroup\$

Unity first uses Axis-Alligned Bounding Boxes (AABB) to check which colliders actually have a chance to collide. Checking two AABB's for intersection is a very cheap operation. Only then does Unity check if the collider-geometries also collide.

That means a mesh-collider only consumes CPU cycles when its bounding box intersects that of another collider. As long as nothing is near it, the collider type doesn't matter much for collision detection performance. Another operation which can cost lots of CPU cycles is rotating, because whenever you rotate an object the engine needs to recalculate the size of its bounding box. By the way, the Unity API exposes the bounding boxes of colliders.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.