2014-12-02 191 views
2

我目前有一個用於建築物的物品放置系統。它通過實例化對象的「鬼魂」來顯示它可以放置的位置,鬼魂(半透明)對象被附加到相機並在玩家點擊時在其位置實例化對象。根據Unity中的地形旋轉對象(C#)

我得到它的位置,以保持鬼對象,像這樣:

 var pos = transform.position + transform.forward * placeDistance; // A position 'someDistance' in front of the player 
     pos.y = Terrain.activeTerrain.SampleHeight(pos); // Get the position at the surface of the terrain to place the object 
     firePlaceable.transform.position = pos + Vector3.up * 0.001f; // 'halfHeight' is used in case the pivot is not on the base 

現在..我所需要的對象根據地形旋轉,使得火的地方或多或少正確放置旋轉。有任何想法嗎?最好的計劃是什麼?

回答

1

在地點的位置使用地形法線矢量。

例如,你可以做一個光線直播從壁爐。結果命中包含一個正常的地方,你的位置是向上的。

想到它......我假設你已經在做一個光線投射來獲得放置壁爐的位置了嗎?

使用佈局raycast獲取向上的矢量,而不是創建一個新的。

所以basicly做

fireplace.transform.up = clickPlaceHit.normal; 
+0

非常感謝!這是我做的: 'Ray ray = new Ray(pos + Vector3.up * 10.0f,Vector3.down); \t \t \t RaycastHit hit = new RaycastHit(); \t \t \t如果(Physics.Raycast(射線,出命中)){ \t \t \t \t firePlaceable.transform.rotation = Quaternion.FromToRotation(transform.up,hit.normal)* transform.rotation; \t \t \t}' – 2014-12-02 15:35:45