Unreliable collision detection between rigidbody and mesh collider

Hi all,

Collisions between my mesh collider and a sphere with a collider and a rigidbody seem to be very unreliable. First off, here’s a build of my project:

http://davidmulqueen.kaen.org/Unity/Builds.html

The maze is an FBX imported from 3DS Max, with a mesh collider. It’s composed of a 16x16 grid of cubes, attached to each other for the base, and extruded upwards to form the walls. The ball is a Unity sphere with a sphere collider and a rigidbody. My code is in FixedUpdate on the maze, and tilts it (yes, it’s horribly inefficient!)…

#pragma strict

var rotationAmount = 1;
var xClamp = 30f;
var yClamp = 30f;
 
function FixedUpdate()
{
	
	//rotating the maze
    if(transform.localEulerAngles.x < xClamp ){  
		transform.Rotate (Input.GetAxis("Vertical") * rotationAmount, 0, 0);
    } else if (transform.localEulerAngles.x > (360-xClamp)){  
       	transform.Rotate (Input.GetAxis("Vertical") * rotationAmount, 0, 0);
    }  
    
    if(transform.localEulerAngles.z < yClamp ){  
    	transform.Rotate (0, 0, Input.GetAxis("Horizontal") * -rotationAmount);           
    } else if (transform.localEulerAngles.z > (360-yClamp)){  
    	transform.Rotate (0, 0, Input.GetAxis("Horizontal") * -rotationAmount);           
    }  

	//clamp rotation
    if(transform.localEulerAngles.z >= yClamp && transform.localEulerAngles.z < (360-yClamp)){  
    	if(transform.localEulerAngles.z < 180){  
        	transform.localEulerAngles.z = (yClamp-0.01);  
        } else{  
        	transform.localEulerAngles.z = ((360-yClamp)+0.01);  
        }  
	}  
	
    if(transform.localEulerAngles.x >= xClamp && transform.localEulerAngles.x < (360-xClamp)){  
    	if(transform.localEulerAngles.x < 180){  
        	transform.localEulerAngles.x = (xClamp-0.01);  
        } else{  
        	transform.localEulerAngles.x = ((360-xClamp)+0.01);  
        }  
    }
    
    if(transform.localEulerAngles.y != 0){  
    	transform.localEulerAngles.y = 0;  
    }

    //debug code - detect if nothing is pressed           
    if ((Input.GetAxis("Vertical") == 0) && (Input.GetAxis("Horizontal") == 0)){
		Debug.Log("Nothing is pressed.");
	}
}

function OnGUI ()
{
	var xRotation = Mathf.Round(transform.localEulerAngles.x * 10) / 10;
	var zRotation = Mathf.Round(transform.localEulerAngles.z * 10) / 10;
	GUI.Box(Rect(10,10,200,130),"X Rotation:"+xRotation+"

Z Rotation:"+zRotation);
}

I’ve reduced fixed timestep to 0.01, I’ve made my sphere collider a little smaller than the ball, and still I continue to have collision issues. Any suggestions would be very welcome!

Mesh colliders can be a bit tricky. If you can, always use basic colliders instead - they’re easier to reason about, less prone to corner cases, and more cost effective. Now, the mesh collider you have on that model should work, but it obviously doesn’t. There could be a number of reasons. If you don’t have a rigidbody attached to the maze, you should - moving objects needs rigidbodies for their collision detection to work properly. Just make sure to turn gravity off and make it kinematic.

If that doesn’t fix it, or you already have a rigidbody attached to the maze, try replacing the mesh collider with box colliders. Simply make a bunch of invisible boxes, give them rigidbodies, and have them match the structure of the maze. Box colliders are faster, and often works better, especially when interacting with mesh colliders, so you might want to do that anyways.