Render order is not correct for mesh renderers?

I have several mesh renderers in a 3D scene, and as they move past each other, they are not necessarily rendering the thing closest to the camera in front. Why is this happening, and how do I fix it?

Here is a gif of the behavior:
render ordering

Here are the meshes’ inspector windows:


Well, I assume that you are using transparent shaders on your meshes. There’s always some sort of issue with transparent meshes, as they need to be sorted. It’s not always possible to sort meshes, as they may easily overlap each other, so sorting is done by some really cheap way, like by a distance of its center to the camera Z plane. While you can have some effect on how this sorting is done, there is no ideal solution and there are always situations that simply can’t be sorted correctly.

I would highly recommend you to use alpha cutout shaders instead. I don’t see any semi-transparency in your scene. Cutoff means that there is still depth write and depth test, which has many benefits

  • No draw sorting needed: Depth buffer is used instead to “sort” per-pixel
  • Performance: Reduces overdraw. Depth test culls fragments early
  • Shadows: Unity projects directional light shadows in screen space using depth information. This can’t work with transparent objects.
  • Post-Processing: Many effects rely on depth as well, same issue as with shadows.
  • Deferred rendering

If you are making a mobile game, then know that cutoff breaks tiled gpu optimizations, and you might be better off modelling the mesh around the holes and using an opaque shader - but it would require an actual benchmark for your specific situation to determine which is better.