You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity C#实现对象实例化时位置递增排列的方法咨询

解决单位生成位置重叠的问题

嘿,我明白你的困扰——现在每次生成单位都挤在同一个 spawn 点,导致它们堆在一起绕圈。要实现每次生成时位置自动递增、依次排列,核心是需要一个能持续记录生成次数的变量,然后用这个变量来偏移每个新单位的 spawn 位置。

下面给你两种实用的解决方案,你可以根据需求选择:

方案1:全局统一排列所有生成的单位

如果所有单位都需要按同一规则依次排列(不管是哪个建筑生成的),可以在你的类里添加一个类级别的计数器:

// 在类的顶部添加这个计数器,不要放在AddUnit方法里
private int _totalSpawnedUnits = 0;

public void AddUnit(string unitName, Vector3 spawnPoint, Vector3 rallyPoint, Quaternion rotation, Building creator) { 
    rallyPoint += new Vector3(-10, 0, 0); 
    Units units = GetComponentInChildren<Units>(); 

    // 关键修改:用计数器偏移spawn位置,这里我用X轴每次偏移2个单位,你可以根据单位大小调整数值
    float offsetPerUnit = 2f; // 每个单位之间的间距,按需调整
    Vector3 spawnOffset = new Vector3(_totalSpawnedUnits * offsetPerUnit, 0, -8);
    Vector3 finalSpawnPoint = spawnPoint + spawnOffset;

    GameObject newUnit = (GameObject)Instantiate(ResourceManager.GetUnit(unitName), finalSpawnPoint, rotation); 
    newUnit.transform.parent = units.transform; 

    Unit unitObject = newUnit.GetComponent<Unit>(); 
    if (unitObject) { 
        unitObject.SetBuilding(creator); 
        if (finalSpawnPoint != rallyPoint) { // 注意这里也要用偏移后的位置判断
            unitObject.StartMove(rallyPoint); 
        } 
    }

    // 生成完成后,计数器加1,下一个单位就会偏移到下一个位置
    _totalSpawnedUnits++;
}

方案2:按生成建筑独立排列单位

如果希望每个建筑生成的单位都从自己的起始位置开始排列(比如兵营A的单位排一列,兵营B的单位排另一列),可以用字典来记录每个建筑的生成次数:

// 在类的顶部添加字典,用来跟踪每个建筑的生成数量
private Dictionary<Building, int> _buildingSpawnCounts = new Dictionary<Building, int>();

public void AddUnit(string unitName, Vector3 spawnPoint, Vector3 rallyPoint, Quaternion rotation, Building creator) { 
    rallyPoint += new Vector3(-10, 0, 0); 
    Units units = GetComponentInChildren<Units>(); 

    // 先获取当前建筑的生成次数,没有的话初始化为0
    if (!_buildingSpawnCounts.ContainsKey(creator)) {
        _buildingSpawnCounts[creator] = 0;
    }
    int spawnCountForBuilding = _buildingSpawnCounts[creator];

    // 按当前建筑的生成次数偏移位置,这里用Z轴举例,你可以换成X轴或者其他方向
    float offsetPerUnit = 2.5f;
    Vector3 spawnOffset = new Vector3(0, 0, -8 + (spawnCountForBuilding * offsetPerUnit));
    Vector3 finalSpawnPoint = spawnPoint + spawnOffset;

    GameObject newUnit = (GameObject)Instantiate(ResourceManager.GetUnit(unitName), finalSpawnPoint, rotation); 
    newUnit.transform.parent = units.transform; 

    Unit unitObject = newUnit.GetComponent<Unit>(); 
    if (unitObject) { 
        unitObject.SetBuilding(creator); 
        if (finalSpawnPoint != rallyPoint) {
            unitObject.StartMove(rallyPoint); 
        } 
    }

    // 更新当前建筑的生成次数
    _buildingSpawnCounts[creator]++;
}

小提示

  • 你可以根据单位的实际大小调整offsetPerUnit的数值,确保单位之间不会重叠
  • 如果想让单位排成行列(比如2个一排,排满换行),可以用除法和取余计算偏移,比如int row = spawnCount / 2; int col = spawnCount % 2;,然后用row和col计算X/Z轴的偏移
  • 如果需要重置计数器(比如建筑被摧毁时),只需要从字典中移除对应的建筑条目,或者把全局计数器设为0即可

内容的提问来源于stack exchange,提问作者ESuth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:45:23