Raycasting then getting the distance of an object

I’ve been working on a simple AI script for the last few days but have just stumbled into a problem… I want to cast a ray of unlimited length, then return the distance once the ray is cast. Is there any way to do this? Thanks.

The RayCastHit-object that gets set during one overload of Physics.RayCast includes the distance from the ray origin to the impact point.

See the documentation here and here.