![](/img/trans.png)
[英]Unity: Change UI gameObject parent and position without moving Child gameObject
[英]Unity Setting Parent Objects position Equal to Their child position
我最終將擁有兩個3D對象,它們將是具有不同大小和形狀的3D模型,但我試圖使基礎功能正確無誤。
一個是常規球體,其子游戲對象位於位置(0,-0.5,0)的s1。
一個是縮放的柱面(0.3,0.3,0.3),子游戲對象位於位置(0,0.5,0)的c1。
我正在嘗試移動對象,以使兩個父對象在相同的子對象位置對齊。
下面的代碼將自行車的位置直接移動到s1位置。 我只是在計算向量數學上遇到麻煩,因此c1位置直接在s1位置。 我嘗試了多種不同的加法,減法,除法以使它們對齊,但沒有任何運氣。 任何了解如何解決此問題的幫助將不勝感激。
public GameObject sphere;
public GameObject cyclinder;
private GameObject s1;
private GameObject c1;
// Start is called before the first frame update
void Start()
{
s1 = sphere.transform.Find("s1").gameObject;
c1 = cyclinder.transform.Find("c1").gameObject;
cyclinder.transform.position = s1.transform.position;
}
盡管您的問題不是很清楚,但我認為您的意思是要以如下方式定位圓柱體和球體:它們的子對象(s1和c1)共享完全相同的世界位置。
我要遵循的步驟是:
例如:
Vector3 finalPos = ... // Position I want both s1 and c1 to share
// Cylinder: world offset, moving from c1 to the parent object
Vector3 cylOffset = cylinder.transform.position - c1.transform.position;
// Position the cylinder, and apply the offset
cylinder.transform.position = finalPos + cylOffset;
// Sphere: world offset, moving from s1 to the parent object
Vector3 sphereOffset = sphere.transform.position - s1.transform.position;
// Position the sphere, and apply the offset
sphere.transform.position = finalPos + sphereOffset;
答案是要做
cyclinder.transform.position = s1.transform.position - (c1.transform.localposition/4);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.