You want to scale the AB segment down to a length of 1 first, then scale that segment by your preferred length.
The Pythagorean theorem will give you the length of AB :
local length = math.sqrt((B.x - A.x)^2 + (B.y - A.y)^2)
So divide both of AB’s components by length, then apply the scale factor to them. Finally, plug them back in as you did before. All together, something like (untested):
local dx = B.x - A.x local dy = B.y - A.y local length = math.sqrt(dx \* dx + dy \* dy) local k = scale / length C.x = A.x - dy \* k C.y = A.y + dx \* k
Note also the swap of dx and dy. If you’re familiar with the slopes of perpendicular lines being inverse reciprocals (i.e. m and -1 / m), this is just that same thing showing up in another form (sometimes called the “perp operator”).