【问题标题】:Spawn number of GameObjects randomly around a circle at a minimum distance from each other围绕一个圆圈随机生成多个游戏对象,彼此之间的距离最小
【发布时间】:2016-12-06 06:20:12
【问题描述】:

目前我有两个球围绕一个圆圈随机生成,它们之间的最小距离为 0.3,因此它们不会发生碰撞。但是,我希望玩家设置要生成的球的数量,并且我需要这些球随机生成,但彼此之间的距离最小。

使用我的代码,任何大于 2 的球数量都可能有两个球相互重叠生成的机会,因为我仅使用 spawnConstant 引用最后一个生成的球的位置。

当我试图用数组来解决这个问题时,我遇到了同样的问题,位置只引用数组中的一个位置。附上当前代码:

IEnumerator SpawnBalls () {

    Vector3 spawnConstant = new Vector3 ();
    Vector3 spawnPosition = new Vector3 ();

    for (int i = 0; i < ballNumber; i++) {

        if (i == 0) {

            spawnPosition = Random.insideUnitCircle.normalized * 0.7f; 
            Instantiate (ball, spawnPosition, Quaternion.identity);
            Debug.Log ("wolo");
            spawnConstant = spawnPosition;

        } else if (i > 0) {

            spawnPosition = Random.insideUnitCircle.normalized * 0.7f;

            while (Vector3.Distance (spawnPosition, spawnConstant) <= 0.3f) {
                spawnPosition = Random.insideUnitCircle.normalized * 0.7f;
            }

            spawnConstant = spawnPosition;
            Instantiate (ball, spawnPosition, Quaternion.identity);
            Debug.Log ("yolo");
        }
    }
    yield return null;
}

我怎样才能让每个球都参考之前所有球的位置并生成离它们最小的距离?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:
    IEnumerator SpawnBalls () {
    
        Vector3[] ballArray = new Vector3[ballNumber];
        Vector3 spawnPosition = new Vector3 ();
    
        int index = 0;
    
        for (int i = 0; i < ballNumber; i++) {
    
            // Create first ball and store coordinates into our ballArray at index 0
            if (i == 0) {
    
                spawnPosition = Random.insideUnitCircle.normalized * 0.7f; 
                Instantiate (ball, spawnPosition, Quaternion.identity);
                Debug.Log ("wolo");
                ballArray[index] = spawnPosition;
    
            // Else we already have one ball
            } else if (i > 0) {
                // Create new spawn coordinates
                spawnPosition = Random.insideUnitCircle.normalized * 0.7f;
    
                // Loop through indicies of array that have been filled
                for( int k = 0; k <= index; k++ ) {
                  // Check that each value in array meets distance requirement
                  if(Vector3.Distance (spawnPosition, ballArray[k]) <= 0.3f) {
                    // If not, create new coordinates and reset k
                    spawnPosition = Random.insideUnitCircle.normalized * 0.7f;
                    k = 0;
                }
    
                // Update array
                ballArray[index++] = spawnPosition;
    
                Instantiate (ball, spawnPosition, Quaternion.identity);
                Debug.Log ("yolo");
            }
            }
            yield return null;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2016-04-13
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多