2
\$\begingroup\$

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.imgur.com/qhGlsuG.png 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 i.imgur.com/kg3M85C.png 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;
    }

}

\$\endgroup\$
  • \$\begingroup\$ What would the intended result look like? \$\endgroup\$ – Anko Jun 27 '15 at 16:38
  • \$\begingroup\$ 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. \$\endgroup\$ – masterpiece Jun 27 '15 at 16:41
1
\$\begingroup\$

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. enter image description here

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – masterpiece Jun 27 '15 at 15:58
  • \$\begingroup\$ 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. \$\endgroup\$ – SanSolo Jun 27 '15 at 16:00
  • \$\begingroup\$ 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. \$\endgroup\$ – SanSolo Jun 27 '15 at 16:02
  • \$\begingroup\$ 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" \$\endgroup\$ – SanSolo Jun 27 '15 at 16:03
  • \$\begingroup\$ Check this image. when pipes reach "x" same as "Destroyer", reset their "x" to "spawner". tinyurl.com/p7tr23y \$\endgroup\$ – SanSolo Jun 27 '15 at 16:08
1
\$\begingroup\$

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

enter image description here

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ this my friend may well be the answer to my problem. Checking it out. \$\endgroup\$ – masterpiece Jun 27 '15 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.