# How to randomly spawn obstacles for infinite runner?

I am trying to create randomly generated path properly? Think of flappy bird (its not really) but my code/algorithm is all wrong. If I make BarMax, BarMin too large, then player can cheat cos in centre there will always be space. Make it too small and pipes are spawned randomly and sometimes very close to each other (unlike flappy). So my player sometimes could run into a situation where its impossible to move past two pipes. My adjacent pipes are really close together as well unlike flappy birds big gap between pipes.

So this is what i dont want to happen. I have also thought about spawning sets of pre-configured pipes but then i thought they might run into this impossible situation at some point too. I had my level set-up like this So when a bar touches the looper, it spawns again - code is above.

int numBGPanels = 35;

float BarsMax = 1.5f; float BarsMin = -1.5f;

float widthOfBGObject = 1; float offset = 0.75f; Vector3 pos;

void Start() {
StartBarsPosition(); }

void OnTriggerEnter2D(Collider2D collider) {

if(collider.tag == "Bars") {       // this is not getting called cos theres no collider attached atm
pos = collider.transform.position;
pos.x += widthOfBGObject * numBGPanels * offset;

pos.y = Random.Range(BarsMin, BarsMax);
collider.transform.position = pos;

}

}

void StartBarsPosition() {
GameObject[] bars = GameObject.FindGameObjectsWithTag("Bars");

foreach(GameObject bar in bars) {
Vector3 initialpos = bar.transform.position;
initialpos.y = Random.Range(BarsMin, BarsMax);
bar.transform.position = initialpos;
}


}

• What would the intended result look like?
– Anko
Jun 27, 2015 at 16:38
• edited post with new links mate. take a look. bascially just need sufficient space between opposite pipes so my player can pass. But currently my code allows them to stay in the centre and ALWAYS avoid the pipes. Jun 27, 2015 at 16:41

How it works in flappy is, all pipes are same size. Their "y" position is altered to create space at random. In this image, red area is the visible screen, green bars are the pipes.

As you can see, all pipes are same size. But by changing their "y" co ordinate, random gaps are generated.

So, in your game, you need minTopY,maxTopY that decides "y" co ordinate of the top pipe, minBottomY,maxBottomY which decides the "y" co ordinate of bottom pipe.

For each pair, "x" co ordinate is same, but "Y" co ordinate is different.

Hope this gives an idea of what you're looking at.

Next, to place each pair of pipes, you call the spawner at random interval of time.

• the problem is in your adjacent pipes, there are big gaps so bird can always use those gaps even when pipes are spawned in a impossible position. But in my game, adjacent pipes are very close together so merely ranodmising y-coordinate sometimes creates situation where path is blocked (impossible to pass). Jun 27, 2015 at 15:58
• You can solve that by calling the spawner function at random intervals. You have minTime,maxTime. Determine those values, call the method at those intervals and you can decide the gap between adjacent pipes. Jun 27, 2015 at 16:00
• Another way is fake the spawning. In this method, what you do is, you have say 50 pairs of pipes. You generate random values and position these pipes manually at those values. Then, when a pair reaches certain position beyond the camera, set it's position to a predetermined point. Jun 27, 2015 at 16:02
• How i do that is, to my camera, i attach a quad as child. When pipes reach same "x" position as this quad, reset their position to the "x" of another quad labelled "spawner" Jun 27, 2015 at 16:03
• Check this image. when pipes reach "x" same as "Destroyer", reset their "x" to "spawner". tinyurl.com/p7tr23y Jun 27, 2015 at 16:08

generate the "holes" using a 1d perlin noise function

• this my friend may well be the answer to my problem. Checking it out. Jun 27, 2015 at 18:10