# Unity3d: Find a point on the line which has shortest distance from the third point

So, initially, I had points A and C. To get point B, I had to shoot a raycast down from point A and get the hit.point, which is point B now. Point D is somewhere between the raycast I shot from A and perpendicular to C. How do I find the point D.

• I answered, but it seems that you don't even need a raycast. What purpose does it have? – trollingchar Aug 7 '19 at 14:13
• Nothing important! I shouldn't have mentioned it! – MrRobot9 Aug 7 '19 at 14:55
• You could use just any second point to define the line, not nessessary the result of raycast. – trollingchar Aug 7 '19 at 16:57

Ok, so your task is actually to project point onto line, defined by two points.

Code:

XY d = a + XY.Project (c - a, b - a);


XY is a 2-dimensional vector with these methods defined:

/// projection of vector v onto vector w
public static XY Project (XY v, XY w) => XY.Dot (v, w) / XY.Dot (w, w) * w;

/// dot product of vectors a and b
public static float Dot (XY a, XY b) => a.X * b.X + a.Y * b.Y;


Also you'll have to implement trivial +-*/ operators.