实例化相对于其父 Unity 的游戏对象

本文关键字:游戏 对象 Unity 相对于 实例化 | 更新日期: 2023-09-27 18:36:36

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

实例化相对于其父 Unity 的游戏对象

嗯,这很简单...

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;
    }
}