Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的游戏对象一直紧挨着彼此繁殖,尽管我给出了一些偏移量_C#_Unity3d_Instantiation - Fatal编程技术网

C# 我的游戏对象一直紧挨着彼此繁殖,尽管我给出了一些偏移量

C# 我的游戏对象一直紧挨着彼此繁殖,尽管我给出了一些偏移量,c#,unity3d,instantiation,C#,Unity3d,Instantiation,我遵循Unity教程中的脚本。我正试着根据我的要求调整它。感谢您的帮助。下面是一幅它是如何出现的图像。我已经给了一个偏移量,以便在生成对象时提供一些空间,但我觉得它不起作用。最小列数=8,最大列数=20 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类ObjectPooler:MonoBehavior { 私有游戏对象池; 公共int columnSize=5; 公共游戏对象列预制; 私有向量2 objectPoolPositio

我遵循Unity教程中的脚本。我正试着根据我的要求调整它。感谢您的帮助。下面是一幅它是如何出现的图像。我已经给了一个偏移量,以便在生成对象时提供一些空间,但我觉得它不起作用。最小列数=8,最大列数=20

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类ObjectPooler:MonoBehavior
{
私有游戏对象池;
公共int columnSize=5;
公共游戏对象列预制;
私有向量2 objectPoolPositionBeforeStart=新向量2(12f,0.42f);
公共浮点数TimesInclastSprowned=4f;
公共浮动利率;
公共浮动columnMin、columnMax;
专用浮点数位置=0.42f;
private int currentColumn=0;
公众浮动抵消;
//用于初始化
void Start()
{
columnPool=新游戏对象[columnSize];
对于(int i=0;i=spawnRate)
{
TimesInclastSpaned=0;
float spawnXPosition=Random.Range(columnMin、columnMax)+偏移量;
columnPool[currentColumn].transform.position=新向量2(spawnXPosition,spawnYPosition);
Log(spawnXPosition);
currentColumn++;
如果(当前列>=列大小)
{
currentColumn=0;
}
}
}
}

我相信你的思路是正确的。我猜您希望列继续远离以前生成的每个列(因此每个新列都有一个相对于前一列的位置)

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

float spawnXPosition=Random.Range(columnMin,columnMax)+偏移量;
这不考虑其他列的位置。也许要将上一列的X位置添加到此值

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

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

我相信您的思路是正确的。我猜您希望列继续远离以前生成的每个列(因此每个新列都有一个相对于前一列的位置)

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

float spawnXPosition=Random.Range(columnMin,columnMax)+偏移量;
这不考虑其他列的位置。也许要将上一列的X位置添加到此值

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

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

如何将一个值应用于
offset
变量?它已经在Inspector中应用,值为5您应该可以通过每次显示spawnxposition轻松证明偏移是否有效。。当然,在开始的时候,你把它们放在同一个地方,你只是在移动“当前列”。。桩体仍将保持在原始位置。。。直到你移动了一整轮物品。。为什么不一开始就把它们放出去呢?与移除初始对象池位置一样?您试图实现什么?您正在尝试实例化彼此之间一定距离内的对象吗?将值应用于
偏移量
变量如何?它已在Inspector中应用,值为5您应该能够通过每次显示spawnxposition轻松证明偏移量是否起作用。。当然,在开始的时候,你把它们放在同一个地方,你只是在移动“当前列”。。桩体仍将保持在原始位置。。。直到你移动了一整轮物品。。为什么不一开始就把它们放出去呢?与移除初始对象池位置一样?您试图实现什么?您是否试图实例化彼此之间一定距离内的对象?正如您所提到的,在生成第一列后,我一直收到一个错误,称为“IndexOutofFrangeException:数组索引超出范围”。这是否意味着我必须增加我的当前列索引?因为您不需要将第一列相对于另一列进行偏移,所以您可以编写一个if检查来查看currentColumn!=0,在这种情况下,如果(currentColumn!=0){spawnXPosition=columnPool[currentColumn-1].transform.position.x+Range(columnMin,columnMax)+offset,则将列偏移量添加到
spawnXPosition
这似乎是我想要的。谢谢,正如您所提到的,在生成第一列之后,我一直收到一个错误,称为“IndexOutOfRangeException:数组索引超出范围”。这是否意味着我必须增加当前列索引?因为您不需要相对于另一列偏移第一列,所以可以
 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;
                }
            }
        }
    }