Vector3.Project, why do none of the examples I see work; why am I not getting this!?
Every time I try to use Vector3.Project; it NEVER WORKS the way I expect. I have tried numerous examples I have found on here, and they never ever work.
I have a line made of points A and B, and a point P that I would like to project on this line. I have tried this:
Vector3 V1 = A - P;
Vector3 V2 = Vector3.Project(V1, A.NormalTo(B));
//return P + V2; this did not work either
return A + V2;
// NormalTo is an extension method I added to get a normal to a point. here is the definition:
public static Vector3 NormalTo(this Vector3 from, Vector3 to) {
return (to - from).normalized;
}
The code was taken from the top post on here for Vector3.Project() (via google search).
This always places the point in some weird place not on the line, but in the proximity of it.
What in the hell am I doing wrong? This function mystifies me.
Answer by maccabbe · Dec 04, 2015 at 04:39 PM
Seems like
Vector3 V1 = A - P;
should be
Vector3 V1 = P - A;
if you're using onNormal = (B - A).normalized;
and adding A back for return A + V2;
Thanks for the answer. I just tried that, but unfortunately it does not solve the problem. I still have the same issue. :/
I take that back. Your solution DOES work. I just had a secondary issue I was unaware of where A and B were not the points I thought they were. Thank you!! If you convert this to an answer, I'll mark it as accepted.
Your answer
![](https://koobas.hobune.stream/wayback/20220612051401im_/https://answers.unity.com/themes/thub/images/avi.jpg)