Search Unity

Infinite Flat Generation

Discussion in 'Scripting' started by PandawanFr, Apr 16, 2015.

  1. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Hello,
    I've searched for hours a way to get this working, but It doesn't work...
    Basically, I want to make a script that would generate series of cube, divided into chunks that would spawn around the player to make an infinite world. These cubes would all have the same y axis, to make it flat. I kind want to reduce lag so maybe a way to not spawn cubes if there already are some.

    Here is my script for now and it's kinda weird...

    Code (CSharp):
    1. public GameObject cube;
    2.  
    3.     void Update () {
    4.         for (int x = (int)transform.position.x; x < 10; x++)
    5.         {
    6.             for (int z = (int)transform.position.z; z < 10; z++)
    7.             {
    8.             Instantiate(cube, new Vector3(x,0,z), Quaternion.identity);
    9.  
    10.             }
    11.         }
    12.     }
    Right now, it's just generating a 10x10 cube platform where the player is, except it's not around it, but in +x and +z, which means that the player is spawned in a corner. Also, weirdly, when I move, the cubes don't generate, I think it has to do with the way I made the for loop, it generates only the first 10 cubes, then it stops because the variable is already at 10, so it doesn't do anything. I can't increase the number too much because it makes the game lag so bad (my computer crashed...)


    Thank you!
     
  2. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Let me restate what I mean because after looking at it, it's not really clear.
    I just want to make a flat world that's made of cubes. And no I can't use planes because I have a script that requires cubes...
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yeah, generating an infinite world based on actual individual cubes is generally not going to work. It will chew through your performance and crash your system pretty quickly. For information on how its done in minecraft google voxel terrain.
     
  4. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    I thought of using six quads that would be placed as a cube, and then not render them when they are not seen but the generator doesn't work well...
     
  5. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    That code is just wrong. Definitely start over.

    You wouldn't want to be doing this in an Update() - this is generating tiles over and over again and will simply kill your system.

    I would initially - maybe in a Start() somewhere or a specifically called function - establish "The World" around the player's initial position.

    Generation of additional tiles would then be tied to player movement - i.e. the player moves "forward" so additional tiles are generated in that direction, and (possibly - depending on your requirements) tiles are removed "behind" the player.
     
  6. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Oh, that's a good idea, thing is that even generating a large amount of tiles (100x100) is laggy. I am probably going to try to reduce the amount and see, I'll just tweak it!