Non-Convex Mesh Colliders in Unity
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:
- Sphere
- Axis-Aligned Bounding Box (AABB)
- Oriented Bounding Box (OBB)
- Discrete Oriented Polytope of n-summits (n-DOP)
- Convex hull (convex mesh in Unity)
- 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:
- AABBTree: similar to OBBTree, but the tree is made of cubes all aligned in the objects’ space. No rotation. It means that for the same precision, you will have a much higher recursion level, hence more cubes, and you will use more resources for physics.
- K-DOP: in term of complexity, K-DOP is efficient but only generates convex hulls. I did not find a pure K-DOPTree packet on the Asset Store, as it seems it is not cost worthy.
- Near-convex hull: this method is based on dividing a non-convex mesh into near-convex parts. The more division you do, the more precision. However, it quickly uses computation time as you are generating more and more convex shapes.
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.