2015-10-28 80 views
0

我正在製作一個統一的3D遊戲,遊戲的一部分允許玩家進入汽車並驅動它。統一遊戲對象不在正確的位置

車內我放置了一個「座位」遊戲對象,其位置和旋轉用於確定玩家將坐在哪裏。

當我想讓玩家坐在車裏時,我讓車成爲父母,然後將玩家轉換位置和旋轉角度設置爲座位。

下面是C#代碼

// make player's parent the same as the seats parent (which is the car) 
transform.parent = seat.transform.parent; 

// put player in the exact position and rotation as the seat 
animator.transform.position = transform.position = seat.transform.position; 
animator.transform.rotation = transform.rotation = seat.transform.rotation; 

animator.transform.localPosition = transform.localPosition = seat.transform.localPosition; 
animator.transform.localRotation = transform.localRotation = seat.transform.localRotation; 

這似乎是它應該工作,但什麼最終happenning是,由於某種原因,球員不能很好地在座位上結束,但記者從一些很短的距離它,也是球員輪換不符合座位,而是稍微偏離。所以這名球員看起來好像並不是真的在座位上,而是在靠近它的位置,然後轉向另一個方向。

有人知道我在做什麼錯嗎?

回答

0

position指定對象在世界上的位置。如果你拿兩個物品並且分配相同的position,那麼他們都會在同一個地方。

localPosition指定對象相對於其父對象的位置。如果您取兩個對象並指定相同的localPosition,則結果將取決於每個對象父級的位置。如果他們有不同的父母,不同的比例或不同的輪換,他們可能會在不同的地方結束。

大致相同的想法適用於rotationlocalRotation

您似乎混淆了世界空間與局部空間座標之間的關係。瞭解這種差異以及它們與場景層次結構的關係非常重要。

如果你想要把在同一個地方的兩個對象,這應該足夠了:

transform.position = seat.transform.position; 
transform.rotation = seat.transform.rotation; 

如果你希望對象跟車移動,後來,你也可以重新分配它的父:

transform.parent = seat.transform.parent; 
0

檢查您的播放器對象的軸心點。它可能不在您期望的位置。如果不是,請嘗試通過拖動模型使其指向所需位置。