Getting Pipes to spawn equal distance from each other in Flappy Bird as pipes speed up.

Just picked up Unity C# and have been following Game Maker’s Toolkit tutorial to make Flappy Bird. I decided to expand further on it by adding difficulty, but the issue is that as the pipes speed up, the distance between the pipes also increases (The tutorial used a fixed timer to spawn in the pipes.)

My plan is to have a 2D Collider (spawnTripwire) ahead of the bird, and whenever a pipe moves through the, it another pipe spawns, mantaining an equal maintaining distance. However, only the initial pipe at void Start() is spawning, and the void OnTriggerEnter2D(Collider2D other) is not going off as expected (located in the newSpawnerBehaviourScript, not the spawnTripwire) I have been trying different methods for several days but nothing seems to work. I have checked that both spawnTripwire and pipe GameObjects have BoxCollider2D components set to trigger.

Current version of my code:

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

public class NewSpawnerBehaviourScript : MonoBehaviour
{
    public GameObject Pipe;
    public float heightOffset = 6;
    public string spawnTripwireTag = "SpawnTripwire";
    public GameObject spawnTripwire;

    // Start is called before the first frame update
    void Start()
    {
        spawnPipe();
    }

    // Update is called once per frame
    void Update()
    {
   
    }
    // function for randomised pipe spawning
    public void spawnPipe()
    {
        float lowestPoint = transform.position.y - heightOffset;
        float highestPoint = transform.position.y + heightOffset;
        Instantiate(Pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
    }

    // spawn pipe whenever pipe enters spawnTripwire
    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.CompareTag(spawnTripwireTag))
        {
            spawnPipe();
        }
    }
}

The basic process is to keep a notion of a “pipe cursor” that you advance ALWAYS by the same amount.

Then each frame you decide if it is time to spawn.

If so, then you:

  • spawn a pipe
  • move the cursor the same precise distance
  • ask if you still need anymore pipes or if you are done this frame.