Hi Everybody,

I’m just taking my first steps into unity and c# after focusing on Python for the past year or so, mostly by following the Unity tutorials. Anyway, I followed the ‘Flappy Bird Style’ game, built it successfully and have been succeeding in adding extra elements to it and learning the code as I’m doing so.

However, I made the GameController increase the scroll speed each successful point scored (this works well, so haven’t included any of the code), but this leads the spawning of columns from the object pool to spawn at increasingly large distances.

I then tried to adjust the spawn rate by decreasing it at the same rate that the score increases, however this ends up with the spawn rate being 0 and every column spawning at once.

Is there a way that I can set the columns to spawn after a set distance from the last spawned column?

If so could anyone let me know/at least point me in the right direction? Thank you in advance, and apologies, I am in my first week of C#.

ColumnPool.cs:

using System.Collections;
using UnityEngine;

public class ColumnPool : MonoBehaviour
{
    public GameObject columnPrefab; //The column game object.
    public int columnPoolSize = 15;
    public float spawnRate = 3f;
    public float columnMin = -1f;
    public float columnMax = 3.5f;
    private GameObject[] columns;
    private int currentColumn = 0;

    private Vector2 objectPoolPosition = new Vector2(-15, -25);
    private float spawnXPosition = 10f;

    private float timeSinceLastSpawned;

    void Start()
    {
        timeSinceLastSpawned = 0f;

        //Initialize the columns collection.
        columns = new GameObject[columnPoolSize];
        //Loop through the collection... 
        for (int i = 0; i < columnPoolSize; i++)
        {
            //...and create the individual columns.
            columns *= (GameObject) Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);*

}
}

void Update()
{
timeSinceLastSpawned += Time.deltaTime;

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

//Set a random y position for the column
float spawnYPosition = Random.Range(columnMin, columnMax);

//…then set the current column to that position.
columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);

//Increase the value of currentColumn. If the new size is too big, set it back to zero
currentColumn++;

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

{
{
spawnRate = 4 - 0.3f * (GameController.instance.score); //<- this is where I am having trouble at the moment.
}
}

}
}

store a reference to the last spawnedwall, then rather checking the timeSinceLastSpawned , just change the if to

if(GameController.instance.gameOver == false && Vector3.Distance(new Vector3(spawnXPosition, spawnYPosition, 0), referenceToTheLastObject) >= desiredDistance) //desiredDistance is the distance to you want the objects to be spawning