Search Unity

Making an Infinite Terrain

Discussion in 'Scripting' started by DebChatterjee, Feb 23, 2016.

Thread Status:
Not open for further replies.
  1. DebChatterjee

    DebChatterjee

    Joined:
    Dec 29, 2015
    Posts:
    9
    I want to make a infinite terrain that when i instantiate roads the road keeps generating (i have that script that generate infinite roads) but the terrain have to follow the road endlessly. how do i script it?
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You going to have to be clearer on this. What type of game is this? Are you looking for a city map generator? Is this just a cross country drive?
     
  3. DebChatterjee

    DebChatterjee

    Joined:
    Dec 29, 2015
    Posts:
    9
    No it's like Zig zag(you can get it in play store) with a man, not ball. It needs infinite terrain and in it, it needs road to be instantiated. So now i have a infinite runner script. Here it is:-
    Code (CSharp):
    1.  
    2. var playerTag : String;
    3. var biome : String;
    4. var length : float;
    5. var isX : boolean;
    6.  
    7. function OnTriggerEnter (col : Collider)
    8. {
    9.     if(col.tag == playerTag)
    10.     {
    11.           if(isX == true)
    12.           {
    13.              col.transform.position.x += length;
    14.           }
    15.           else
    16.           {
    17.              col.transform.position.z += length;
    18.           }
    19.      BiomeChanger.curBiome = biome;
    20.     }
    21. }
    22.  
    and including i have a biome changer script. And here is the biome changer:-
    Code (CSharp):
    1.  
    2. var desert : GameObject;
    3. var hills : GameObject;
    4. var isBiome : boolean;
    5. static var curBiome = "desert";
    6.  
    7. function Start () {
    8.  
    9. }
    10.  
    11. function Update () {
    12. if(isBiome == true){
    13. if(curBiome == gameObject.name){
    14. }
    15. else{
    16. gameObject.SetActive(false);
    17. }
    18. }
    19. else{
    20. if(curBiome == "desert"){
    21. desert.SetActive(true);
    22. }
    23. if(curBiome == "hills"){
    24. hills.SetActive(true);
    25. }
    26. }
    27. }
    It's a complicated process. I get it from here;-

    so Now i have to instantiate raods in it. So what can i do now? it needs Jumping, sliding in the player. the player will go as the ball goes in zig zag.
    i hope you understand what i means. Thanks
     
  4. matthewseaward

    matthewseaward

    Joined:
    Apr 12, 2013
    Posts:
    50
    I made a top down shooter some time back that required seemingly endless terrain. I think resizing a single terrain could hit performance if the game goes on for a long time, you'll have a very large terrain.

    What I did was to create a series of small terrains, say 5 terrains 100x100 in size and just instantiated them as the player got to the edge of the map. I could random which ones were created and delete older ones so there was at most only 2 small terrains on display.

    Maybe something to look at?
     
    DebChatterjee likes this.
  5. DebChatterjee

    DebChatterjee

    Joined:
    Dec 29, 2015
    Posts:
    9
    okay got it, but assume if I want to instantiate a city in desert, then? means, i am playing through the desert and after some time(lets's take 2 minutes) i want to instantiate a city in the desert, then what to do? i have made a city in my 2nd terrain (the first one have desert.) And i want to do it randomly.
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    First off, I'd suggest pooling over instantiating and destroying. I'd have a pool of terrain meshes and then cycle throw these as needed. The other thing you could look into would be pooling the meshes and changing the mesh heights based on a height map as needed. I have not tried this, so I cannot say how performant it would be. This would also depend upon the target platform.

    If you wanted to add a city, this also depends upon the game. If the city takes up all of the game view, then you would have a change in your code that would instantiate the city prefabs instead of the terrain prefabs; if you wanted to have the city in the terrain, then I'd spawn then I'd spawn the city as a terrain detail on top of the terrain, as if I were adding trees, signs or whatever to my terrain.

    Perhaps you need to get started and then start asking questions on a more detailed level.

    By the way, was that series on procedural terrain meant for runtime? Or just edit time?
     
    Alverik and DebChatterjee like this.
  7. DebChatterjee

    DebChatterjee

    Joined:
    Dec 29, 2015
    Posts:
    9
    It's for runtime. Basically I want to make a infinite runner mobile game, so it needs city and a infinite terrain. That's why I need it and what is my plan is to instantiate my city buildings on the left and right side of the path, like set their anchor points to the left and right sides. Is it a good to do this? And Thanks.
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm not sure if we are all talking cross purposes on the word "terrain". I take terrain to mean the terrain component in Unity, which may be far more complicated than can easily be used for a mobile game.

    The usual solution is to make a series of "tiles" made from geometry. By tile, in this case, we mean a regular sized chunk of geometry with entry and exit points. Each tile is created to join up with the previous tile. These get pooled and hidden in the game, and then the pieces are enabled and positioned in the scene in a random order as the player runs down the track.

    Now, whether the tile has a city theme or country theme or desert theme is entirely up to you, they just need to join and transition to the previous tile.
     
    Kiwasi likes this.
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    basically think "junior jigsaw puzzle"

    you might want to look at

    from around 33:00 (unity forums doesn't like time indexed links to youtube vids apparently :( )
    it covers the concept of "sockets" in procedural terrain generation (it's a dungeon example, but your endless running is just a long line of terrain rather than a sprawling one)
     
    Last edited: Feb 24, 2016
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Not very performant using the native terrain. It mostly comes down to the set heights call. It's typically enough to drop a frame.
     
  11. DebChatterjee

    DebChatterjee

    Joined:
    Dec 29, 2015
    Posts:
    9
    But how can i rapidly instantiate city and desert between 3 or 4 minutes gap?
     
  12. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You need to create a block system which you can instantiate parts, not generate each part. This will be alot faster than you think. If this is an infinite driver, you are going to have a big problem if you keep generating stuff, especially if the user turns around.

    The jigsaw puzzle method works great. You can also use Procedural randomization to handle turn arounds.

    Imagine that you have a X and Y for a position of a puzzle...

    Code (csharp):
    1.  
    2. int r = (int)Mathf.floor(( x * 1619 * y * 31337 * 1013 % 0xffffffff) / 0xffffffff * (float)numPieces);
    3.  
    4. // 1619, 31337 and 1013 are all prime numbers, so the values usually are never the same twice.
    5.  
    so this basic random generator gives you a number based off the x and y coordinate you feed it.

    so this looks like this (x = 5, y = 10, numPieces = 50)...
    Code (csharp):
    1.  
    2. a = 5 * 1619;
    3. b = 10 * 31337;
    4. c = a * b * 1013;
    5. c = 2569707641950;
    6. d = c % 4294967295; (1317199540)
    7. e = 1317199540 / 4294967295; (0.30668441679018654)
    8. f = Mathf.floor(e * 50); (15)
    9.  
    10. // so you use part 15 at x: 5, y: 10 (always)
    11.  
    This is the basics of 3d generated noise.
     
  13. DebChatterjee

    DebChatterjee

    Joined:
    Dec 29, 2015
    Posts:
    9
    Do you have something easy to understand? just want to instantiate some game objects(like in the temple run changing scenes and instantiating some obstacles and barriers ). Just give some examples or video links. Thanks
     
  14. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,153
  15. dvxl

    dvxl

    Joined:
    Mar 13, 2020
    Posts:
    2
    That was exactly what i was looking for, can you pls help me with this? If you have discord, you can add me dvxl#4344
     
  16. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    Don't necropost. Come on now...
     
    matkoniecz likes this.
Thread Status:
Not open for further replies.