2016-02-27 32 views
-1

我想實例化一個對象相對於它的父親。爲了更加精確,我需要在將要移動和旋轉的平面表面上實例化一個立方體。謝謝您的幫助!實例化相對於它的父Unity的gameObject

+0

示例代碼將是很好的解決。或者只是嘗試使用本地偏移量將立方體實例化爲平面的子體,這樣做。 – CarbineCoder

+0

我試過這個:'public GameObject cube; Quaternion rotoffset; Vector3 posoffset; \t \t void Start(){ rotoffset = transform.rotation; posoffset = transform.position;實例化(cube,posoffset,rotoffset); '但是我不知道如何從這個位置正確地移動對象。 –

回答

1

嗯,這是很簡單......

public class Spawner : MonoBehaviour 
{ 
    public void SpawnChild(GameObject prefab, Vector3 relativePosition, Quaternion relativeRotation = Quaternion.identity) 
    { 
     GameObject childObj = Instantiate(prefab); 
     childObj.transform.parent = transform; 
     childObj.transform.localPosition = relativePosition; 
     childObj.transform.localRotation = relativeRotation; 
     childObj.transform.localScale = Vector3.one; 
    } 
} 
相關問題