你能生成游戏对象并将它们移动到最后一个添加的 x 位置吗

Can you spawn gameobjects and move them to an x position that is added by the last one

我是 Unity 游戏开发的新手,我很好奇是否有任何方法可以在某个位置生成游戏对象,然后在该位置添加的下一个游戏对象是 1,然后下一个将是 2 然后3等等。

在 inspector 中,您需要分配 prefab 并设置 objectCountspacing 值。这将从零开始沿正 x 轴生成对象。如果要沿不同的轴移动对象,请将 position.x 中的 x 更改为其他 vector3 组件之一 (y, z)。要沿相反方向沿轴移动,请反转间距值(例如,从 1 到 -1)。

using UnityEngine;

public class ObjectSpawner : MonoBehaviour
{
    public GameObject prefab;
    public int objectCount;
    public float spacing;

    void Start()
    {
        var position = new Vector3();

        for (int i = 0; i < objectCount; i++)
        {
            Instantiate(prefab, position, Quaternion.identity);
            position.x += spacing;
        }
    }
}