- Home /
Programming techniques: Long rail type levels (seamless terrain)
I am currently thinking about the best way to load the terrain of a rail type shooter. Obviously placing all the terrain elements in the Editor is not the best approach.
I was thinking about generating new instances of terrain as we get close to the end of the current one.
public GameObject TerrainPrefab;
void AddTerrain(Vector3 pos)
{
GameObject fire = (GameObject)Instantiate(TerrainPrefab, pos, Quaternion.identity);
}
But that causes small fps drops which might not be noticeable if you have an almost empty scene but it might cause lag spikes in the final game where every bit of performance important. Making it asynchronous would work but I dont know if thats even possible with Unity.
Second approach I could think of is only having 2 instances of terrain and after passing the previous one it gets moved after the current one and the new terrain gets loaded into the terrain data. But then again I am not sure if swapping the terrain data would essentially require the same amount of calculation as the first approach.
EDIT: Now I saw that Unity has a LoadLevelAdditivieAsync function but that is for the PRO version only which I cant afford yet. Actually I only need to load the terrain asynchronous.
I am fairly new to Unity and also in general to game development so I would like to get some opinions from experienced people.
What do you think? What might be the best approach? Is it possible to do it asynchronous in a new thread or something like that in Unity Free to counter lag spikes?
Answer by save · Jul 06, 2011 at 08:27 AM
Have a look at this free Infinite Terrain project. There's also the possibility to just move the player and its comforting space and relocating at the very origin point when the track runs out (instead of moving everything else, which is far more intense).
Unity runs all coroutines in a single thread where some features, already defined by the engine, is multi-threaded. You cannot tell Unity what to multi-thread. Although not to confuse with multitasking, which Unity is great at. For future versions of Unity (with v3.3 as current) more things will be capable of multi-threading. Version 3.5 will have a multi-threaded renderer for instance - which will take some load off of everything else.
Here's some more information about infinite/seamless terrains:
http://answers.unity3d.com/questions/9633/possible-infinite-terrain-plane.html
http://answers.unity3d.com/questions/17225/dynamic-terrain-loading.html
Thanks for your answer. Though I still have a few questions:
Do you mean like relocating every seen object to the origin?
Does that mean that it is not possible to load terrain async without Unity pro? And moving terrain and stuff like that is never done in the background no matter what?
How far can we go into one direction, not relocating to the origin, without causing problems? 100.000 meters? more/less?
Also I came across StartCoroutine and wrote the following code. What do you think? is it necessary to yield return on every line? Is it slow to check for terrainData != TerrainData and only then assign it?
IEnumerator $$anonymous$$oveTerrain()
{
if (TerrainA.terrainData != TerrainData[terrainIndex])
TerrainA.terrainData = TerrainData[terrainIndex];
yield return new WaitForSeconds(1);
SetTerrainNeighbors(TerrainB, TerrainA);
yield return new WaitForSeconds(1);
TerrainA.transform.position = TerrainB.transform.position + new Vector3(0, 0, 2000);
}
1 & 3: At some point you'll have to relocate the player as Unity starts getting physic artifacts at ~20k units away from the world origin (0,0,0). This is due to the single-precision floating point that Unity uses to locate and position objects.
You can use logic where you move everything around the player for really massive scenes, instantiate objects in front of the player and remove them behind the player, above camera culling distances.
The best implementation differs quite vastly depending on the situation for your games behavior.
2: You shouldn't have to load levels additive if you just want to create a seamless world - but it all depends on what your game and structure demands.
Hopefully someone else can fill you in a bit further if you're able to give a bit more detail about how the logic should work in your game:
Is it 3d/2d?
Is it a one-way track?
Is the player always moving with an object or can it get off, similar to a train-ride?
Your answer
