1 回答

TA貢獻1796條經驗 獲得超4個贊
有幾種表達這個問題的方法。
一種是沿 B 的上軸找到這樣一個點,使其與 A 成直角。這將忽略 A 的任何旋轉。
為此,將 A 的位置沿著 B 的位置向上投影。換句話說,求出 (AB) 和 B's up 的點積,將其乘以 B's up,然后將其與 B 相加。
Vector3?cPos?=?B.transform.position?+?Vector3.Dot(A.transform.position?-?B.transform.position,?B.transform.up)?*?B.transform.up; C.transform.position?=?cPos;
另一種方法是找到 B 向上和 A 向右的交點。這可能會形成非直角,或者根本沒有點,具體取決于 A 和 B 的旋轉。
這個有點復雜,
public static bool LineLineIntersection(out Vector3 intersection, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){
? ? ? Vector3 lineVec3 = linePoint2 - linePoint1;
? ? ? Vector3 crossVec1and2 = Vector3.Cross(lineVec1, lineVec2);
? ? ? Vector3 crossVec3and2 = Vector3.Cross(lineVec3, lineVec2);
? ? ? float planarFactor = Vector3.Dot(lineVec3, crossVec1and2);
? ? ? //is coplanar, and not parrallel
? ? ? if(Mathf.Abs(planarFactor) < 0.0001f && crossVec1and2.sqrMagnitude > 0.0001f)
? ? ? {
? ? ? ? ? float s = Vector3.Dot(crossVec3and2, crossVec1and2) / crossVec1and2.sqrMagnitude;
? ? ? ? ? intersection = linePoint1 + (lineVec1 * s);
? ? ? ? ? return true;
? ? ? }
? ? ? else
? ? ? {
? ? ? ? ? intersection = Vector3.zero;
? ? ? ? ? return false;
? ? ? }
? }
然后找到你的位置:
Vector3 cPos;
bool doIntersect = LineLineIntersection(out cPos, A.transform.position, A.transform.right, B.transform.position, B.transform.up);
if (doIntersect) {
? ? C.transform.position = cPos;
} else {
? ? // do something reasonable, like using projection, or not changing the position
? ? Vector3 cPos = B.transform.position + Vector3.Dot(A.transform.position - B.transform.position, B.transform.up) * B.transform.up;
? ? C.transform.position = cPos;
}
- 1 回答
- 0 關注
- 196 瀏覽
添加回答
舉報