The following code works differently in Unity 3.x and 4.x:
function Update () {
var hit: RaycastHit;
var mousePos = Input.mousePosition;
var theRay = camera.main.ScreenPointToRay(mousePos);
if (!Physics.Raycast(theRay, hit, 10))
return;
if (hit.collider == null)
return;
print (hit.collider);
}
In Unity 3.5.7, I only see output when the mouse is over a collider in the scene. It prints the name of the object the collider is attached to. Otherwise, it never gets to the print line.
In Unity 4.1.2, I see output continuously, even when the mouse is outside the game window. When the mouse is not over a collider in the scene or outside the game window, it prints “Null” (not “null”). If the mouse is over a collider in the scene, it prints the name of the object the collider is attached to, as it does in 3.x. The Raycast function always returns true.
Is there any way to get this to work like it used to, so it only executes when a collider is hit?