- Home /
Infinite Terrain Dilemma. (Chunk Based System... Not A Minecraft Clone) [FIXED! PLEASE USE AS A REFERENCE.]
I cannot think of a solution. My problem is that my terrain gen is working but the chunks are not loading the way I want. They are only loaded beneath me, which makes sense because that is how I wrote the code but I do not know how to spawn more "chunks" in. Like, (i'll use minecraft as an example) when you move in the world, chunks infront of you become generated and the ones behind you removed. The problem is that in mine, chunks only spawn in when they are beneath you and then the last chunk gets deleted. There is also another problem, my chunk system makes the game very laggy and when a new chunk becomes generated, there is roughly a 7 second freeze. My computer isn't the best (far from it) but I know it is not supposed to be that bad.
I know my code is quite messy and poorly written, I didn't really pseudo-code but this could have been better. I didn't want to download any scripts or follow a youtube tut with this game. I want the effort put in. Idk, it feels more original that way. But Unity Answers is fine with me.
<<<<<<<<<<UPDATE>>>>>>>>>>
I fixed it in my own time.... I needed to practically revamp the code. I changed everything and here is what I have now. Not that the script is split into two segments. The Old Segment and The New Segment:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Chunks : MonoBehaviour {
/*<<<<<<<<<<OLD CODE>>>>>>>>>>*/
// public GameObject SeaChunkMain;
// public GameObject SeaChunkSurr;
// float currentPosX;
// float currentPosZ;
//
// public List<Vector3> alreadySpawnedChunks = new List<Vector3>();
//
//
// void Start()
// {
// Instantiate(SeaChunkMain, new Vector3(0, 0, 0), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(0, 0, 0));
//
// for(int x = 0; x < 20; x++)
// {
// for(int z = 0; z < 20; z++)
// {
// if(!alreadySpawnedChunks.Contains(new Vector3(0 + x, 0, 0 + z)))
// {
// Instantiate(SeaChunkSurr, new Vector3(0 + x, 0, 0 + z), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(0 + x, 0, 0 + z));
// }
//
// if(!alreadySpawnedChunks.Contains(new Vector3(0 - x, 0, 0 + z)))
// {
// Instantiate(SeaChunkSurr, new Vector3(0 - x, 0, 0 + z), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(0 - x, 0, 0 + z));
// }
//
// if(!alreadySpawnedChunks.Contains(new Vector3(0 - x, 0, 0 - z)))
// {
// Instantiate(SeaChunkSurr, new Vector3(0 - x, 0, 0 - z), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(0 - x, 0, 0 - z));
// }
//
// if(!alreadySpawnedChunks.Contains(new Vector3(0 + x, 0, 0 - z)))
// {
// Instantiate(SeaChunkSurr, new Vector3(0 + x, 0, 0 - z), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(0 + x, 0, 0 - z));
// }
// }
// }
// }
//
// void Update ()
// {
// currentPosX = Mathf.Round(transform.position.x);
// currentPosZ = Mathf.Round(transform.position.z);
// ChunkInstanciator(currentPosX, currentPosZ);
// ChunkInstanciator(currentPosX, currentPosZ);
// }
//
// void ChunkInstanciator(float cPX, float cPZ)
// {
// if(!alreadySpawnedChunks.Contains(new Vector3(cPX, 0, cPZ)))
// {
// if(GameObject.FindGameObjectsWithTag("Chunk") != null)
// {
// for(int i = 0; i < GameObject.FindGameObjectsWithTag("Chunk").Length; i++)
// {
// alreadySpawnedChunks.Remove(GameObject.FindGameObjectsWithTag("Chunk")[i].transform.position);
// Destroy(GameObject.FindGameObjectsWithTag("Chunk")[i]);
// }
// }
// Instantiate(SeaChunkMain, new Vector3(cPX, 0, cPZ), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(cPX, 0, cPZ));
//
// for(int x = 0; x < 20; x++)
// {
// for(int z = 0; z < 20; z++)
// {
// if(!alreadySpawnedChunks.Contains(new Vector3(cPX + x, 0, cPZ + z)))
// {
// Instantiate(SeaChunkSurr, new Vector3(cPX + x, 0, cPZ + z), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(cPX + x, 0, cPZ + z));
// }
//
// if(!alreadySpawnedChunks.Contains(new Vector3(cPX - x, 0, cPZ + z)))
// {
// Instantiate(SeaChunkSurr, new Vector3(cPX - x, 0, cPZ + z), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(cPX - x, 0, cPZ + z));
// }
//
// if(!alreadySpawnedChunks.Contains(new Vector3(cPX - x, 0, cPZ - z)))
// {
// Instantiate(SeaChunkSurr, new Vector3(cPX - x, 0, cPZ - z), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(cPX - x, 0, cPZ - z));
// }
//
// if(!alreadySpawnedChunks.Contains(new Vector3(cPX + x, 0, cPZ - z)))
// {
// Instantiate(SeaChunkSurr, new Vector3(cPX + x, 0, cPZ - z), Quaternion.identity);
// alreadySpawnedChunks.Add(new Vector3(cPX + x, 0, cPZ - z));
// }
// }
// }
// }
// }
/*<<<<<<<<<<NEW CODE>>>>>>>>>>*/
public GameObject SeaGO;
public List<Vector3> chunks = new List<Vector3>();
float PX;
float PZ;
public float chunkSize = 4;
void Start()
{
ChunkInst(0, 0);
}
void Update()
{
PX = Mathf.Round(transform.position.x);
PZ = Mathf.Round(transform.position.z);
ChunkInst(PX, PZ);
}
void ChunkInst(float valueX, float valueZ)
{
//Destroys
for(int d = 0; d < GameObject.FindGameObjectsWithTag("Chunk").Length; d++)
{
Destroy(GameObject.FindGameObjectsWithTag("Chunk")[d]);
chunks.Clear();
}
for(int i = 0; i < chunkSize; i++)
{
for(int z = 0; z < chunkSize; z++)
{
// chunks.Add(Instantiate(SeaGO, new Vector3(valueX - i, 0, valueZ), Quaternion.identity) as GameObject);
// chunks.Add(Instantiate(SeaGO, new Vector3(valueX + i, 0, valueZ), Quaternion.identity) as GameObject);
// chunks.Add(Instantiate(SeaGO, new Vector3(valueX, 0, valueZ - i), Quaternion.identity) as GameObject);
// chunks.Add(Instantiate(SeaGO, new Vector3(valueX, 0, valueZ + i), Quaternion.identity) as GameObject);
if(!chunks.Contains(new Vector3(valueX + i * 10, 0, valueZ + z * 10)))
{
Instantiate(SeaGO, new Vector3(valueX + i * 10, 0, valueZ + z * 10), Quaternion.identity);
chunks.Add(new Vector3(valueX + i * 10, 0, valueZ + z * 10));
}
if(!chunks.Contains(new Vector3(valueX - i * 10, 0, valueZ - z * 10)))
{
Instantiate(SeaGO, new Vector3(valueX - i * 10, 0, valueZ - z * 10), Quaternion.identity);
chunks.Add(new Vector3(valueX - i * 10, 0, valueZ - z * 10));
}
if(!chunks.Contains(new Vector3(valueX + i * 10, 0, valueZ - z * 10)))
{
Instantiate(SeaGO, new Vector3(valueX + i * 10, 0, valueZ - z * 10), Quaternion.identity);
chunks.Add(new Vector3(valueX + i * 10, 0, valueZ - z * 10));
}
if(!chunks.Contains(new Vector3(valueX - i * 10, 0, valueZ + z * 10)))
{
Instantiate(SeaGO, new Vector3(valueX - i * 10, 0, valueZ + z * 10), Quaternion.identity);
chunks.Add(new Vector3(valueX - i * 10, 0, valueZ + z * 10));
}
}
}
}
}
Also note that the newer version runs alot faster.
Answer by $$anonymous$$ · Oct 13, 2014 at 06:57 PM
The Gameobject's Scale Has To Be: X: 1, Y: Whatever (Recommended 0.1f), Z: 1