# Raycasting against my plane does not detect collisions in Unity

I'm trying to keep the camera above a plane which acts as the ground, so I wrote this bit of code:

RaycastHit hit;
if (!Physics.Raycast (transform.position, new Vector3 (0, -50, 0), out hit, 5)) {
transform.position = new Vector3 (0, 20, 0);
Debug.Log ("No ground found when raycasting down!");
Debug.DrawRay (transform.position, new Vector3 (0, -50, 0), Color.red, 0, true);
} else {
if (hit.distance <= 1) {
transform.position = new Vector3 (transform.position.x, 1, transform.position.z);
}
}


The camera is clearly over the plane on position 0, 20, 0 while the plane is at 1, 1, 1, with scale 50, 1, 50, but it never detects a hit. The ray drawn by DrawRay() does intersect the plane.

The plane is oriented upwards, but just to be sure I tried raycasting from below as well, without success.

I have also tried replacing the mesh collider with a box collider using size 10, 1, 10 (I'm not sure why, this seems to be the same size as my scaled plane) and 10, 10, 10. Neither worked.

What Am I missing?

• Are your colliders set as triggers? – hobnob Jul 25 '16 at 9:47
• @hobnob I have tried both with and without setting them as triggers – Annonymus Jul 25 '16 at 9:47
• Is the ray hitting any other objects? Have you tried RaycastAll instead? – jgallant Jul 25 '16 at 9:57

I found the problem while modifying my code to try out Jon's sugestions. Somehow I added a max distance of 5 to the RayCast. Must have gotten confused with the many overloads or something.
if (!Physics.Raycast (transform.position, new Vector3 (0, -50, 0), out hit/*, 5*/)) {