相对于其父Unity实例化gameObject

时间:2016-02-27 22:00:05

标签: c# unity3d scripting game-engine

我想实例化一个相对于其父对象的对象。更确切地说,我需要在平面的表面上实例化一个立方体,该立方体将被移动和旋转。谢谢你的帮助!

1 个答案:

答案 0 :(得分: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;
    }
}