distance between a ray and a point

How do I calculate the distance between a ray and a point ?

DaveA, it’s too complicated
here is the solution

public static float DistanceToLine(Ray ray, Vector3 point)
    return Vector3.Cross(ray.direction, point - ray.origin).magnitude;

public static float DistancePointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
return Vector3.Magnitude(ProjectPointLine(point, lineStart, lineEnd) - point);
public static Vector3 ProjectPointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
Vector3 rhs = point - lineStart;
Vector3 vector2 = lineEnd - lineStart;
float magnitude = vector2.magnitude;
Vector3 lhs = vector2;
if (magnitude > 1E-06f)
lhs = (Vector3)(lhs / magnitude);
float num2 = Mathf.Clamp(Vector3.Dot(lhs, rhs), 0f, magnitude);
return (lineStart + ((Vector3)(lhs * num2)));


if your point and line are two-dimensional,
i have an extremely efficient routine for calculating the distance between them,
documented here.

in general for questions like this, it’s hard to beat Paul Bourke.

float GetDistance(Vector3 rayOrigin, Vector3 rayDir, Vector3 point){
float distance = Vector3.Distance(rayOrigin,point);
float angle = Vector3.Angle(rayDir,point - rayOrigin);
return (distance * Mathf.Sin(angle * Mathf.Deg2Rad));