Non-Convex Mesh Colliders in Unity

in Unity non-convex mesh colliderUniversal Colliders

As of today (2021), Unity struggles with colliders, making a nightmare for game designers. It should not be. This page introduces the problem and gives you solutions.

The Problem

You roughly have the choice between a box, a sphere or a capsule for efficient computation. If you really want precision, Unity offers the mesh collider option, much slower, limited to 256 triangles, and … for convex colliders only.

Why is it so limited? The answer is: effectiveness and simplicity. In video games, collisions between convex shapes are privileged because they are simple to compute. Here is a brief list of most common colliders:

  1. Sphere
  2. Axis-Aligned Bounding Box (AABB)
  3. Oriented Bounding Box (OBB)
  4. Discrete Oriented Polytope of n-summits (n-DOP)
  5. Convex hull (convex mesh in Unity)
  6. Non-convex hull

See the Wikipedia page over bounding volumes.

The more complex it is, the more precision you get, but collision detection becomes costly. You should scarcely use convex mesh colliders in Unity because it generates lots of tests, and non-convex shape even more scarcely, this is why they can’t be used with rigidbodies.

Solutions

Keep it simple, stupid

— KISS principle

It may seem a constraint, but those simple shapes are a good way to make the games running smoothly. In fact, you should avoid using mesh colliders at a maximum. Keep in mind that 16 boxes are always more efficient than a convex mesh collider of 256 triangles. In fact, you should only use spheres, capsules and boxes.

However, setting up 16 boxes by hand is a nightmare. This is why we created the Universal Collider Asset, which makes good use of Unity types to generate sets of colliders. We use a method called OBBTree, which makes things as smooth as possible.

Why is this solution better? The Asset Store provides other packages using AABBTree, K-DOP, and near-convex hulls. I will cover each of these:

Capsules and other primitives

What about capsule colliders? It is a common shape in video games, memory efficient, so Unity added it. One could think of other primitives (tetrahedrons, cones, dodecahedron, etc…) but Unity decided there was enough types.

Afterthoughts

As it appears, Unity did not really change its collision components through years. However, the game engine could really get buffed with an improved colliders generation system, as colliders and triggers are used in (almost) every game.