亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Unity3d:定位游戲對象,使其與其他兩個游戲對象形成直角三角形

Unity3d:定位游戲對象,使其與其他兩個游戲對象形成直角三角形

C#
阿晨1998 2023-07-09 10:21:29
如何定位C gameObject使其與 形成直角三角形A and B。我嘗試使用(B.transform.position.x,A.transform.position.z),但它仍然給了我一些接近的東西A(它在全球范圍內使用)。我希望C沿著local red axis of A和local green axis of B如圖所示。我該怎么辦?
查看完整描述

1 回答

?
慕的地8271018

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;

}


查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 196 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號