This is quite simple if we use a implicit line equation,
because then we need the normal to the direction vector, which is simply the vector that the unit is facing. Lets call this vector A, then we compute A=(ax,ay) as
ax=sin(getdir unit)
ay=cos(getdir unit)
next we need a point on the line, lets call it P=(px,py):
px=getpos unit select 0
py=getpos unit select 1
Next we need the position of the unit whose shortest distance to the line you want, lets call it R=(rx,ry)
rx=getpos dude select 0
ry=getpos dude select 1
Ok here we go: We have a normal vector A of the line, a point P which is
on the line, and another point R whose distance to the line we want to
compute.
The implicit line equation of the perpendicular line now simply is
(X-P)*A=0
where * is the 2-dimensional dot product, and X is any point on the line.
(note: the dot product of two vectors U and V is computed as (ux*vx+uy*vy)). If the point X we stick into the equation is ON the line, then the result is 0. If its not, the result will be a positive or negative value d. Now we plug in the point R:
(R-P)*A=d
and the shortest distance s you're looking for now is
s=((R-P)*A)/|A|
(this is due to the fact that the dot product equals the cosine of the angle between the two vectors, multiplied with their length. in this case the cosine is exactly what we need to find the length of the side opposite to the angle)
ok here's the code, I've set W=(wx,wy)=R-P for simplicity:
wx=rx-px
wy=ry-py
s=(wx*ax+wy*ay)/sqrt(ax*ax+ay*ay)
hope it works