endless randomly generated environment

Hi,
After being a visitor for a while, i finally register myself on the forum.

I was wondering how to achieve an endless environment in Unity.
I’m a JS newbie and Unity newbie and i see some topic about this but no one really help me to understand what i need to do.

I want my character to continuously run forward in a random endless environment, attacking enemies that come in front of him and jumping on plateform to avoid holes.

I understand the Theory, prefab pieces instantiate in front of the player while those behind the player are destroyed.

What i miss is how technically doing this. I know how to instantiate and destroying the prefabs but i don’t know the best way to randomize them.

what should i do ?

Thanks to everyone for your answers :slight_smile:

How you randomize them is highly dependent on your level design. For example, if it’s acceptable to any element at any time, you can just use Mathf.Random() to index a collection of all your level geometry pieces. If you have constraints, like piece X can only be followed by piece Y, but anything can follow piece Z, you’ll need additional logic to track and manage those constraints.

Start implementing it, get as far as you can, and then post specific questions with example code when you get stuck. It’s very hard for people to answer general questions like this without a lot of extra information.

Thank you Laurie for your answer.
I’ll post what i have done when i go home.
I made 3 spawnergameobject at 0,10 and 20 on Z ( my prefabs are 10 unite large) and the spawners translate on -Z when the reach -10 the prefab is killed, and the spawner is sent at 20 on Z with a new instantiate prefab randomly choose with a Random.Range.
I have couple of problems with this.
First, the spawner overlap each other over time, the don’t translate at the same speed, when i remove the Time.deltatime on the translation it works but i don’t think it’s a good idea to remove it.
The other problem is my character ( a sphere with basic input script). he stay on the prefab and following them on their movement. I try to put the collider on " is kinetic" but the sphere can"t collide with wall or jump on platform like this.

More info when i’ll be on my computer .

There was a recent thread discussing exactly the first problem you describe; unfortunately I can’t recall what it was called. The solution, though, is to realize that since Update() is running at discrete intervals, your level geometry is unlikely to be at the exact position you’re assuming when you spawn the next piece; instead of spawning at a fixed location, calculate a time-corrected location (i.e. figure out the correct position based on Time.time and the movement speed of your level geometry) and spawn there, so you know it’s in the correct position.

For your second problem, it sounds like perhaps you need to apply a forward impulse to your character to (partly or fully) compensate for movement due to friction with the level geometry, but it’ll be easier to figure that out based on looking at the code and a fuller explanation of what’s happening.

It does depend on your game design. Often what you want in a platform game is the character stays in place and the background scrolls, so it appears infinite, but really the character is moving very little.

Ultimately, a character can’t move infinitely in the game. The math starts to break down around a few million units from the origin, so you’ll need to design some kind of limit in your game.

Thanks for your answers !
I don’t had the time for testing your method Laurie, i’ll test it this weekend.

I already have my character at 0 and the world scrolling. The character only have limited mouvement to avoid enemies.

here are the code i have on my gameobjects :

var block01 : Transform;

var block02 : Transform;

var block03 : Transform;

var childCount : int;



function Update () {

	if(transform.childCount <= 0) {
	

var randomBlock : int = Mathf.Abs(Random.Range (1,4));


var spawnedblock : Transform;



if(randomBlock == 1){

spawnedblock = block01;

}


else if(randomBlock == 2){

spawnedblock = block02;

}


else if(randomBlock == 3){

spawnedblock = block03;

}


var chosenBlock : Transform = Instantiate(spawnedblock, transform.position, transform.rotation);

chosenBlock.parent = transform;

}

}

and this one for make them moving :

function Update () {



transform.Translate(0,0,Time.deltaTime * varHolder.speed);



if ( transform.position.z <= -10){



transform.position.z = varHolder.backpos;

}


}

This one kill the prefab when they go behind the character :

function Update () {


if ( transform.position.z <= -9){



Destroy(gameObject);

}


}

I also have a varHolder script for all the static variable i use in multiple script like speed and backpos who is the position where the gameobject go when he reach -10.