Hello,
I’ve searched for hours a way to get this working, but It doesn’t work…
Basically, I want to make a script that would generate series of cube, divided into chunks that would spawn around the player to make an infinite world. These cubes would all have the same y axis, to make it flat. I kind want to reduce lag so maybe a way to not spawn cubes if there already are some.
Here is my script for now and it’s kinda weird…
public GameObject cube;
void Update () {
for (int x = (int)transform.position.x; x < 10; x++)
{
for (int z = (int)transform.position.z; z < 10; z++)
{
Instantiate(cube, new Vector3(x,0,z), Quaternion.identity);
}
}
}
Right now, it’s just generating a 10x10 cube platform where the player is, except it’s not around it, but in +x and +z, which means that the player is spawned in a corner. Also, weirdly, when I move, the cubes don’t generate, I think it has to do with the way I made the for loop, it generates only the first 10 cubes, then it stops because the variable is already at 10, so it doesn’t do anything. I can’t increase the number too much because it makes the game lag so bad (my computer crashed…)
Let me restate what I mean because after looking at it, it’s not really clear.
I just want to make a flat world that’s made of cubes. And no I can’t use planes because I have a script that requires cubes…
Yeah, generating an infinite world based on actual individual cubes is generally not going to work. It will chew through your performance and crash your system pretty quickly. For information on how its done in minecraft google voxel terrain.
You wouldn’t want to be doing this in an Update() - this is generating tiles over and over again and will simply kill your system.
I would initially - maybe in a Start() somewhere or a specifically called function - establish “The World” around the player’s initial position.
Generation of additional tiles would then be tied to player movement - i.e. the player moves “forward” so additional tiles are generated in that direction, and (possibly - depending on your requirements) tiles are removed “behind” the player.
Oh, that’s a good idea, thing is that even generating a large amount of tiles (100x100) is laggy. I am probably going to try to reduce the amount and see, I’ll just tweak it!