The documentation on RaycastHit2D.fraction is factually incorrect.
It states:
First of all, this doesn’t jive with the results I’m getting. Even when inputting a normalized direction vector to the ray, the fraction output is always a fraction of 1, and not the actual hit distance (unless the input distance is equal to exactly 1.0).
Secondly, it claims the fraction is in relation to the direction vector’s magnitude, but this also doesn’t seem to be the case - it seems to be a fraction of the distance float input to the ray instead (which makes a lot more sense, in reality).
I think the documentation is assuming distance = 1 even though that is not stated anywhere.
EDIT: Below I describe how one actually gets a proper hit distance out of the fraction.
I thought maybe it would work if I first normalized the direction, then multiplied that direction by the input distance before doing the cast. But that doesn’t seem to help, either. It still just outputs a fraction of 1 and not the actual distance.
The only thing that works, as far as I can see, is first normalizing the input direction, THEN multiplying the fraction output by the input distance.
Someone should really clean up that documentation because I have already seen incorrect implementations of a distance output by other coders, ie in the Playmaker 2D actions. They make the same assumptions regarding the documentation that I did.