Is there a limit on the distance of raycast?

I’m using this line of code:

if (!Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit)) 
	return;

And the purpose is to detect if the mouse cursor touched a mesh collider.

And it works just fine on the vast majority of objects. But when I tried it on a REALLY large sphere (the size of a small planet), the above line of code no longer works.

Is there a limit on the distance of either physics.raycast or Camera.main.ScreenPointToRay? And if so, is there a way to increase the distance?

No there isn’t. If you don’t provide a distance it will default to float.PositiveInfinity. So the ray will literally have a length of “infinity”. Raycasting doesn’t work by really shooting a ray which will “travel” though the space. The physics system direclty tests the intersection of the ray with any bounding box and finally with the actual colliders of the objects.

Keep in mind that if your ray starts inside a collider and goes “out” of the collider you won’t hit the collider. You can only hit a collider on the outside.

if (!Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit, 100))
return;

your ray is limited to distance of 100 here in world space