Does anyone know what the relationship is between the performance of Physics.Raycast, and the amount of triangles in a MeshCollider? Will it take longer for more polygons, or is Unity doing some fast, octree culling behind the scenes?
I ask because I have a large terrain with a MeshCollider attached to it, and use Raycast to detect collision with it. However, most of the terrain will never be touched, and I was wondering whether it would be worthwhile to split then mesh into touchable and untouchable submeshes and only attach the MeshCollider to the touchable bits.
The terrain mesh is around 30-40k triangles, and this is on iPhone. And yes, we have determined that this is important for performance.
Thanks in advance.
Very large mesh colliders do slow things down, as the physics must internally check against every triangle, sort of - by what ratio it is slower, I am not sure. Of course there are lots of internal optimizations, but you'd have to read up on Physx here
The best way to find out of course is a simple test - create one mesh collider with 40k triangles, and then create 1 with 4k, and check out the performance difference in unity 2.6 with the profiler.
If you can, you might also try using a compound collider with a bunch of box colliders, unless the detail in this terrain is exceptionally important.
I used the CharacterController (which internally does some raycasting) for ExtremeSledding on the iPhone, typically on terrains of ~5k triangles. If the terrain got to around 10k, performance was horrible. I can't imagine you will have much luck with 40k triangles on the iPhone except in the simplest of cases.
It goes without saying, but it would be worthwhile to split the terrain simply for the purposes of culling, if that makes sense in your game. (I assume you already are, and simply kept a single mesh collider?). At around 12k triangles on a 3G (not 3gs...) performance starts to sag a bit for me.
answered Dec 22 '09 at 05:31 PM
For a test, I'd want to have the same number of collision triangles in the scene but have them in more than one collider. That'll tell you if Physx is doing any internal optimization like Bounding Box checking. I hope that it is. :)
answered Dec 22 '09 at 08:03 PM
I know I'm in late, but you can also create a simplified version of the path you want your player to be able to take through the terrain and use that as collision. Turn off the Mesh Renderer and group it with your visible polygons.
answered Jan 30 '10 at 10:25 PM
You can Go through this site for Physics.Raycast .. http://unity3dnotes.blogspot.com/2011/07/how-to-use-physicsraycast-in-your-game.html
answered Jul 12 '11 at 05:15 AM