【问题标题】:Instantiating gameObject relative to its parent Unity相对于其父 Unity 实例化游戏对象
【发布时间】:2016-06-11 02:23:49
【问题描述】:

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

【问题讨论】:

  • 示例代码可以很好地修复。或者只是尝试将立方体实例化为具有局部偏移的平面的子级,这样就可以了。
  • 我试过这个:public GameObject cube; Quaternion rotoffset; Vector3 posoffset; void Start () { rotoffset = transform.rotation; posoffset = transform.position; Instantiate(cube, posoffset,rotoffset); } 但我不知道如何从这个位置正确移动对象。

标签: c# unity3d scripting game-engine


【解决方案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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2017-11-25
    • 1970-01-01
    • 2021-10-18
    • 2016-02-16
    • 1970-01-01
    相关资源
    最近更新 更多