尽管我给出了一些偏移量,但我的游戏对象一直紧挨着生成

My Gameobjects keep spawning right next to each other although I gave some offset

我遵循了其中一个 Unity 教程中的脚本。我正在尝试根据我的要求进行调整。任何帮助表示赞赏。下面是它如何显示的图像。我已经给出了一个偏移量,以便在生成对象时提供一些 space,但我觉得它不起作用。列最小值 = 8,列最大值 = 20

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ObjectPooler : MonoBehaviour
    {

        private GameObject[] columnPool;
        public int columnSize =5;

        public GameObject columnPrefab;

        private Vector2 objectPoolPositionBeforeStart = new Vector2(12f, 0.42f); 

        public float timeSinceLastSpawned = 4f;
        public float spawnRate;
        public float columnMin, columnMax;
        private float spawnYPosition = 0.42f;
        private int currentColumn = 0;
        public float offset;
        // Use this for initialization

        void Start()
        {

            columnPool = new GameObject[columnSize]; 
            for (int i = 0; i < columnSize; i++)
            {
                columnPool[i] = Instantiate(columnPrefab, objectPoolPositionBeforeStart, Quaternion.identity);

            }
        }

        // Update is called once per frame
        void Update()
        {

                    timeSinceLastSpawned += Time.deltaTime;

          if (GameController.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
            {

                timeSinceLastSpawned = 0;
                float spawnXPosition = Random.Range(columnMin, columnMax) + offset;
                columnPool[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
                Debug.Log(spawnXPosition);
                currentColumn++;

                if(currentColumn >= columnSize)
                {
                    currentColumn = 0;
                }
            }
        }
    }

我相信你绝对是在正确的轨道上。我猜你希望列继续走得更远,对于之前生成的每一列(这样每个新列都有一个相对于前一列的位置)。

目前,这是确定列 X 位置的代码。

float spawnXPosition = Random.Range(columnMin, columnMax) + offset;

这不考虑其他列的位置。也许您想将上一列的 X 位置添加到该值?

float spawnXPosition = columnPool[currentColumn - 1].transform.position.x + Random.Range(columnMin, columnMax) + offset;

编辑:只需确保 columnPool[currentColumn - 1] 不超出范围!您不需要第一列的任何原始偏移量:)