c# - 从一个位置实例化尽可能多的游戏对象并在另一个位置结束

标签 c# unity3d

如何实例化尽可能多的从一个位置开始到另一个位置结束的游戏对象。例如,在 x=0 处实例化 gameObject,并在 x=5 轴处结束。在这两个值之间,应该有尽可能多的游戏对象,最好是 10-12 个小规模的。

public GameObject prefab;

void Awake()
{
    GameObject ref = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
}

最佳答案

当您说尽可能多的游戏对象时,我猜您的意思是没有重叠?

假设 prefab 使用 Collider,此解决方案有效。

我总是会实例化第一个对象,然后简单地获取它的边界框,这样我们就知道它有多大

 var first = Instantiate(prefab, Vector3.zero + Vector3.right * MinX, Quaternion.identity);

var bounds = new Bounds(Vector3.zero, Vector3.zero);
foreach (var col in first.GetComponentsInChildren<Collider>(true))
{
    bounds.Encapsulate(col.bounds);
}

// now you can get the size in X direction
var width = bounds.size.x;

我怀疑你的预制件的枢轴点可能在中心,所以首先将它向右移动其宽度的一半

first.transform.position += Vector3.right * width / 2f;

现在您可以检查有多少对象适合您的给定范围。让我们说例如宽度为 1 然后在从 05 的范围内总共有 4 个对象。计算中会有一些冗余(添加 1 然后减少 1 等)但我会留下它以便更好地理解

var minPosition = MinX;
var maxPosition = MaxX;

var actualMinPosition = minPosition + width / 2;
var actualMaxPosition = maxPosition - width / 2;

// +1 here since before we reduced actualMinPosition and actualMaxPosition by 
// exactly 1 * width
var possibleAmount = (int)Mathf.Floor((actualMaxPosition - actualMinPosition) / width) + 1;

所以现在实例化缺失的对象

// since I guess you also want them evenly spread between the start and end position
var distanceBetween = (actualMaxPosition - actualMinPosition) / (possibleAmount - 1);

//因为我们已经实例化了第一个 //我们只生成 possibleAmount - 1 more 对于 (var i = 0; i < possibleAmount - 1; i++) { //+1 因为我们以 i=0 开始循环但第一个 //这里的对象实际上是总共第二个生成的对象 //所以我们希望它已经被移动 var x = actualMinPosition + distanceBetween * (i + 1);

        var obj = Instantiate(prefab, Vector3.zero + Vector3.right * x, Quaternion.identity);
    }

一起来

public void Spawn()
{
    var first = Instantiate(prefab, Vector3.zero, Quaternion.identity);

    var bounds = new Bounds(Vector3.zero, Vector3.zero);
    foreach (var col in first.GetComponentsInChildren<Collider>(true))
    {
        bounds.Encapsulate(col.bounds);
    }

    // now you can get the size in X direction
    var width = bounds.size.x;

    first.transform.position += Vector3.right * width / 2f;


    var minPosition = MinX;
    var maxPosition = MaxX;

    var actualMinPosition = minPosition + width / 2;
    var actualMaxPosition = maxPosition - width / 2;

    // +1 here since before we reduced actualMinPosition and actualMaxPosition by 
    // exactly 1 * width
    var possibleAmount = (int)Mathf.Floor((actualMaxPosition - actualMinPosition) / width) + 1;


    // since I guess you also want them evenly spread between the start and end position
    var distanceBetween = (actualMaxPosition - actualMinPosition) / (possibleAmount - 1);

    // since we already instantiated the first one
    // we spawn only possibleAmount - 1 more 
    for (var i = 0; i < possibleAmount - 1; i++)
    {
        // +1 here since we started the loop with i=0 but the first
        // object here is actually the second to be spawned in total
        // so we want it to be moved already
        var x = actualMinPosition + distanceBetween * (i + 1);

        var obj = Instantiate(prefab, Vector3.zero + Vector3.right * x, Quaternion.identity);
    }
}

enter image description here

我只是在 Update 中为这个演示销毁并重新生成了所有内容

关于c# - 从一个位置实例化尽可能多的游戏对象并在另一个位置结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56463739/

相关文章:

c# - .Net 图表将日期显示为一周中的几天

c# - 没有指定输入参数的 Moq 模拟方法

c# - IAudioSessionControl2 出现问题,使 WebBrowser 控件静音

android - Unity3d Android Java堆空间错误

c# - unity navmesh ai卡住了

unity3d - 统一着色器图 : How to speed up time from Time node?

C# 'invert' 日期时间列表

c# - 在 servicestack 中使用前导 @ 字符反序列化 JSON

c# - 什么是 `invalid token in class` 错误?

c# - 具体来说,在 Unity 中, "where"是否真的等待返回?