2017-03-04 72 views
1

在統一中我試圖讓我的遊戲對象正在使用的精靈的支點。Unity:如何獲得遊戲對象的精靈的樞軸?

我在遊戲中將遊戲對象(我們稱之爲A)放置在遊戲中時,我更改了精靈的支點。現在,當我嘗試在A的變換中產生另一個對象時,新的遊戲對象,並且我知道這很熱它應該這樣做,產生於A的左上角。我需要以某種方式計算出世界座標中正在使用的精靈A的中心。

我嘗試這樣做:

Debug.Log(tempGameObj.GetComponent<SpriteRenderer>().sprite.pivot); 

但這產生的結果如(-80,0,400),而所述樞轉點(-0.25,1.25)。

我該如何計算中心?

回答

2

你要找的是什麼ScreenToWorldPoint方法

Vector3 center = Camera.main.ScreenToWorldPoint(tempGameObj.GetComponent<SpriteRenderer>().sprite.pivot); 

這裏的documantation的情況下,你需要它 https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

+1

這是正確的。 ['Sprite.pivot'](https://docs.unity3d.com/ScriptReference/Sprite-pivot.html)以像素形式返回,需要'ScreenToWorldPoint'將其轉換爲正常的世界位置。根據OP在做什麼,OP可能還必須使用['InverseTransformPoint'](https://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html)將世界pos轉換爲Sprite的本地位置。 – Programmer

+0

@Halil,請記住,如果這解決了您的問題,請不要忘記通過接受它來解決問題。 – Programmer