Shortest distance from a point to a vector

I have a ball velocity (wihout end point) and a player. I want to calculate the shortest distance (perpendicular distance) between the player and the ball velocity/direction.

I know it uses dot product, but I need further explanation as other sites do not have what I want.


You can’t calculate shortest distance from a point to a vector. You can calculate distance from a point to a line (Ray in unity), since a vector denotes either direction or position, but not both at the same time. So any vector can basically pass through any point, the distance being zero.

If you want to find the distance between a point and a line, you can find your answer here.

To sum it up:

Vector3 direction = rigidbody.velocity.normalized;
Vector3 startingPoint = transform.position;

Ray ray = new Ray(startingPoint, direction);
float distance = Vector3.Cross(ray.direction, point - ray.origin).magnitude;

Edit: Forgot to normalize the velocity.

If you are looking for shortest path from point to line segment you can use: HandleUtility.DistancePointLine