Basic 3D Maths/Trigonometry problem

(I tried to post this in unity answers but It wouldn’t let me post it, and I don’t know why)

Hey, I just started working on a game in unity and came across a problem. I have very little experience with 3d Maths and only barely remember Trig/Highschool maths.

Here is my maths problem:

A = Waypoint

B = Waypoint

The character moves from A to B in a straight line and at a constant speed. However, if the player uses the mouse to drag the character, the character then speeds up until it reaches point Y. At which point it slows back down to its original constant speed.

X = Where the player clicked (dragged)

I know the position of A,B and X.

How would I find the position of Y (in written maths) and how is it done within unity scripting(C# preferred)? Lastly, is there any resources that you can recommend to learn about these things?

Thanks :smile:

Bump?

If I understand correctly, you want to find the perpendicular projection of X onto the line segment AB. This is actually quite easy:

Vector3      a, 
             b, 
             x, // a, b and x are the values you know.
             ab = b - a, // ab is the vector a -> b
             ax = x - a, // ax is the vector a -> x
             ay = Vector3.Project(ax, ab), // the vector a -> y is found by projecting ax onto ab
             y = a + ay; // add the vector ay to the position a to find y
// y now contains the position you look for

Please note that I did not test this, so it may contain errors. I believe it to be correct, though ;).

That is exactly what I wanted, thank you! I didn’t know there was a function like project -_- This would of saved me so much time if I knew about it before haha