Hi, I ran into another mathematical problem there…
I have a script setting up the equation for a straight 2D-line (y = k*x + t) passing through to points, but then I need another script to determine the point on this line which is closest to the player. I thought of somehow projecting a vector A onto a vector B, A being a vector going from the starting point of the line to the player’s position and B being a directional vector of the line.
Anyway, I couldn’t really figure out a way of how this can be done, is there a simple solution to this problem?
This is just a matter of orthogonality. You need a slope which intersects your current grape at 90 degrees and passes through the point of interest. If k is fixed, then your slope is k and the perpendicular line would be -1/k. If k is not a fixed value, it’s kind of similar, but more complicated.
Okay, I just found a solution using Vector3.Project… Seems like I wasn’t searching for it long enough and askied for help to early…
But what’s really frustrating is the fact that your proposition involves exactly the same mathematical stuff I had in an exam last week. Maybe I should revise more frequently…
I asked the question some time ago, so i’m not entirely sure what code I was using, but the method wasn’t very reliable. In the end (as I refered to earlier), I just reused the stuff from my math lessons.
The easiest way to find the point on a line that’s closest to a specific point in space is to draw a line perpendicular to the other line, but passing through your point. We are looking for the closest point on that first line, so just check for an intersection between the two lines. That exact spot is the point you’re looking for.
I guess back then, I used a pretty complicated script that somehow ended up using Vector3.Project(). It must have given me some approximation of the correct result or I wouldn’t have done it that way, but I really suggest using the solution involving perpendicular lines passing through a point.