每次创建对象时尝试增加实例化位置UNITY C#

时间:2018-05-09 20:18:01

标签: c# unity3d instantiation

我想知道如何设置以下代码以在每次创建对象时增加实例化位置,因此它们彼此相邻排列。目前他们只是为同一个地方而战,并开始在一个圈子里互相跳舞。

public void AddUnit(string unitName, Vector3 spawnPoint, Vector3 rallyPoint, Quaternion rotation, Building creator)
    {
        rallyPoint += new Vector3(-10, 0, 0);
        Units units = GetComponentInChildren<Units>();
        spawnPoint += new Vector3(0, 0, -8);
        GameObject newUnit = (GameObject)Instantiate(ResourceManager.GetUnit(unitName), spawnPoint, rotation);
        newUnit.transform.parent = units.transform;
        Unit unitObject = newUnit.GetComponent<Unit>();

        if (unitObject)
        {

            unitObject.SetBuilding(creator);
            if (spawnPoint != rallyPoint)
            {

                unitObject.StartMove(rallyPoint);

            }
        }
    }

1 个答案:

答案 0 :(得分:2)

这很简单。只需移动此代码

即可
    spawnPoint += new Vector3(0, 0, -8);

到AddUnit()函数之外,改为调用它。

每次调用函数时都要增加起始位置。这是一个例子。

void Update()
{
    spawnPoint += new Vector3(0, 0, -8);
    AddUnit(unitName, spawnPoint, rallyPoint, rotation, creator);
}