Making an Infinite Terrain

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?

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?

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:-

var playerTag : String;
var biome : String;
var length : float;
var isX : boolean;

function OnTriggerEnter (col : Collider)
{
    if(col.tag == playerTag)
    {
          if(isX == true)
          {
             col.transform.position.x += length;
          }
          else
          {
             col.transform.position.z += length;
          }
     BiomeChanger.curBiome = biome;
    }
}

and including i have a biome changer script. And here is the biome changer:-

var desert : GameObject;
var hills : GameObject;
var isBiome : boolean;
static var curBiome = "desert";

function Start () {

}

function Update () {
if(isBiome == true){
if(curBiome == gameObject.name){
}
else{
gameObject.SetActive(false);
}
}
else{
if(curBiome == "desert"){
desert.SetActive(true);
}
if(curBiome == "hills"){
hills.SetActive(true);
}
}
}

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

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?

1 Like

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.

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?

2 Likes

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.

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.

1 Like

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 :frowning: )
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)

Not very performant using the native terrain. It mostly comes down to the set heights call. It’s typically enough to drop a frame.

But how can i rapidly instantiate city and desert between 3 or 4 minutes gap?

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…

int r = (int)Mathf.floor(( x * 1619 * y * 31337 * 1013 % 0xffffffff) / 0xffffffff * (float)numPieces);

// 1619, 31337 and 1013 are all prime numbers, so the values usually are never the same twice.

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)…

a = 5 * 1619;
b = 10 * 31337;
c = a * b * 1013;
c = 2569707641950;
d = c % 4294967295; (1317199540)
e = 1317199540 / 4294967295; (0.30668441679018654)
f = Mathf.floor(e * 50); (15)

// so you use part 15 at x: 5, y: 10 (always)

This is the basics of 3d generated noise.

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

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

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

Don’t necropost. Come on now…

1 Like