RaycastHit2D Distance from Hit Object

Hi,

I’m using a RaycastHit2D to determine whether or not my Ray has intersected with a GameObject. I’m trying to find the distance between my Ray’s origin and the hit point. After searching through the docs and numerous sites, it appears the RaycastHit2D object has no such ‘distance’ property, unlike the 3D RaycastHit (see RaycastHit Distance). I need to know the length of the Ray between my origin and the intercepted object as I am using this to apply calculations on a Line Renderer.

Below is a graphic explaining the situation.


(hit variable is the RaycastHit2D object)

Thanks,

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