- Home /
Move and Repeat Terrain
I have set up a Terrain in the editor with a main camera pointing down the Terrain from the start of it.
I need some how for the Terrain to move towards the camera slowly, but the problem I am facing is to get the Terrain to repat so it looks like it is never ending.
So once the Terrain starts to move towards the camera the same Terrain will have to be created and joined to the end of the first so they keep moving towards the camera seemlesley.
The best way I can think to describe it is kind of like a Tredmill.
Answer by Kryptos · Apr 18, 2012 at 12:32 PM
You can duplicate the terrain then disconnect the terrain when one goes out of view and move this same terrain down (also out of view) and reconnect.
@Nicolas$$anonymous$$usset That sounds like exactly what I need, is there a chance you can start me off with this, kind of a noob with Unity still :)
I know that my answer is too short. It is just an idea. Unfortunately I don't have the time to write a proper answer. I hope somebody will help you do that.
Answer by T27M · Apr 18, 2012 at 02:44 PM
Create a prefab with the terrain in it and a rigidbody attached. Remove gravity. Create a cube and set it to a trigger. (1,1,1) Scale is fine.
Position your camera, You will have to tweak this so you don't see the ground plane spawning.
Move the cube behind your camera. Make sure the cube is level with the ground plane so in my tests both were Y = 0. Mine is at 256,0,-245.
Create an empty object. I placed mine at 0,0,0 my terrain was set to 512 by 512 so I put my camera at 256,5,0. Half way between the terrain.
Create a javascript and attach it to the cube you created and enter this code.
// Terrain Prefab
var terrain : GameObject;
//Spawn Point
var spawn : GameObject;
//Speed at which the ground mvoes
var power : float = 1000;
// calls the spawnTerrain function and spawns a terrain when the game starts
function Start () {
spawnTerrain();
}
//Crates the terrain from the prefab and applies movement.
function spawnTerrain ()
{
var spawn = Instantiate(terrain, spawn.transform.position, spawn.transform.rotation);
spawn.rigidbody.AddForce(Vector3(0,0,-power));
}
// when the terrain collides with the cube a new one is spawned and the current one is destroyed
function OnTriggerEnter (other : Collider)
{
Debug.Log("Spawn");
Destroy(other.gameObject,2);
spawnTerrain();
}
Interesting idea. But instantiating terrain can be quite expensive. You better instantiate two terrains at the beginning and recycle them.
Hmm yeah youre right, It shouldn't be hard to modify todo that.
Thanks for the help so far, home can I modify the code to be like @$$anonymous$$ryptos answer
I think you need to make the terrain not-static, does this have problems?