Dolzen
August 31, 2015, 11:06pm
1
In a Vector3 world, I have point A and point B, and all I want to do is get point C as you can see in this image, I know it’s possible but may require advanced math techniques that I don’t have, The only reason I want to do this is because I want to rotate a gameobject in pont A in the direction of point B but only in his local Y axis, thanks guys!
This is the code I’m using to rotate game object in pont A in the direction of point B:
pointA.rotation = Quaternion.Slerp(
pointA.rotation,
Quaternion.LookRotation((pointB - pointA).normalized),
Time.deltaTime
);
Im not 100% sure, but I think you might be looking for cross…
1 Like
@JamesLeeNZ Yup if you want to get point C in world space like the diagram states, you should use Cross like James has listed.
Maybe the ClosestPointsOnTwoLines method found here can help.
http://wiki.unity3d.com/index.php/3d_Math_functions
Ill paste it here in case the link dies
Click for code
public static bool ClosestPointsOnTwoLines(out Vector3 closestPointLine1, out Vector3 closestPointLine2, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){
closestPointLine1 = Vector3.zero;
closestPointLine2 = Vector3.zero;
float a = Vector3.Dot(lineVec1, lineVec1);
float b = Vector3.Dot(lineVec1, lineVec2);
float e = Vector3.Dot(lineVec2, lineVec2);
float d = a*e - b*b;
//lines are not parallel
if(d != 0.0f){
Vector3 r = linePoint1 - linePoint2;
float c = Vector3.Dot(lineVec1, r);
float f = Vector3.Dot(lineVec2, r);
float s = (b*f - c*e) / d;
float t = (a*f - c*b) / d;
closestPointLine1 = linePoint1 + lineVec1 * s;
closestPointLine2 = linePoint2 + lineVec2 * t;
return true;
}
else{
return false;
}
}
You may want to edit that code to instead of returning bool and using out, to just return what you want.
Kiwasi
September 1, 2015, 5:37am
5
The cross product returns a vector that is perpendicular to both the vectors passed in. Not sure this will help you.
I’m struggling to see how A, B and C are related to each other…