2D raycast distance is incorrect?

The following code should display the distance between the origin and the impact point, but instead it shows the length of the ray minus the actual distance.

RaycastHit2D hit = Physics2D.Raycast( transform.position, -Vector2.up, 10.0F );
Debug.Log(hit.distance);

this code gives me the correct value

RaycastHit2D hit = Physics2D.Raycast( transform.position, -Vector2.up, 10.0F );
Debug.Log(10 - hit.distance);

Is this a bug or is there something different about 2d raycasts…?

I can confirm that RaycastHit2D.distance gives incorrect results. And I think it’s a bug.

RaycastHit2D.point gives a correct hit point. Hence, a reliable way to obtain the correct distance is:

float distance = Vector2.Distance( raycast2D.point, transform.position );

or

float distance = ( raycast2D.point - (Vector2) transform.position ).magnitude;

Hope this helps.

This was driving me insane, but I figured it out. RaycastHit2D.fraction returns the number I was expecting (the collision point).

I ran into a very similar problem and finally discovered that Raycast2D appears to be sensitive to the Transform scale in which you are running it. In my case, a parent object was set to Scale 0.5, making my collision detection appear to happen at half the distance it should.

At least something to check if you’re facing similar issues.

I can confirm that RaycastHit2D.distance (down) returns wrong value.