When I was trying to answer a question about collision normal inaccuracies, I performed some tests with colliders and normals, and found a very strange behaviour (see movie here).
I basically put a simple cylinder to rotate along its axis, and cast a perpendicular ray to its center, then drew the resulting normal at the hit point. Ideally, the normal should return over the incident ray, but it began to swing like a pendulum!
The small project containing the tests can be download here . If someone wants to reproduce this experiment from scratch, just create a simple cylinder and attach to it the following script:
var height:float = 0;
var length:float = 5;
function Update(){
var org:Vector3 = Vector3(-length,height,0);
var hit:RaycastHit;
Debug.DrawRay(org,Vector3.right*length,Color.red);
if (Physics.Raycast(org,Vector3.right,hit,length)){
var w:float = (hit.point-org).magnitude;
Debug.DrawRay(hit.point,hit.normal*w,Color.green);
}
transform.Rotate(0,30*Time.deltaTime,0);
}
It’s necessary to click the Gizmos button over the Game view in order to see the rays.
While the cylinder rotation is set to (0,0,0), the normal returns over the incident ray, as expected. If you set the X rotation to 90, however, the mistery appears: the normal begins to swing smoothly in a large angle. Things go even worse if you set the initial rotation to (0,0,90): not only the normal swings in a bigger angle, but also the hit point oscillates back and forth!
Can anyone explain this mistery? Am I doing something wrong? Or is it a bug?
NOTE: This strange behavior only affects the capsule collider. Box, sphere or mesh colliders behave as expected despite the rotation angle.