Chunk Optimization

Can somebody tell me how i can make the chunk to respawn at a new location and deleted at another location like this:
2088909--136607--WorldGeneration.gif

My chunk script today are 2 simple for loops :stuck_out_tongue:
Thx to everyone how helps me!

void Start ()
    {
        int index = 0;
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                GameObject chunkClone = Instantiate(chunk, new Vector3(x * (cOffsetX -1), y * (cOffsetY -1), 0), Quaternion.Euler(rotation)) as GameObject;
                chunkClone.transform.parent = this.transform;
                chunkClone.name = "Chunk " + index;
                index++;
            }
        }
    }

Make a list of your instantiated objects.

using System.Collention.Generic;

private List<GameObject> m_allTiles = new List<GameObject>();
private float visibleDistance = 20.0f; // not sure how big your map size so you will need to adjust this

// in your loop add chunkClone to the list, m_allTiles.Add(chunkClone);

// then once per second or less check the distance between player and tiles
private IEnumerator CheckDistanceAndHide() {
    while(true) {
        yield return new WaitForSeconds(0.3f);

        for(int i=0;i<m_allTiles.Count;i++) {
            float distance = Vector3.Distance(player.transform.position,m_allTiles[i].transform.position);
            if(distance>visibleDistance) {
                m_allTiles[i].SetActive(false);
            } else {
                m_allTiles[i].SetActive(true);
            }
    }
}

// then add StartCouroutine(CheckDistanceAndHide()); after you instantiate all the tiles.

Note that my checking is pretty slow in terms if you have huge map and can give spikes. If you have a huge map you will have to divide map into a smaller chunks and check tiles only in chunk that player is in.

1 Like

for now that worked :slight_smile: thanks!
but i have another question for you ^^

i have created a procedural mesh (chunk) with the marching cube algorithm and now i want to add a collider to it…

question 1. which collider should i take (mesh collider? or polygon collider?)
question 2. how can i add the mesh to the collider… i’ve tried everything but it didn’t work :confused:

  1. Mesh Collider for 3D, Polygon Collider for 2D.
  2. Add mesh to the collider? I don’t get this.

okey, my problem is that i have a terrain consisting of chunks and my chunks can look like that


you see that on the left side i have some vertices and on the right side too… so my questions are:

  1. should i use polygon collider 2D for my 2D mesh?
  2. should i add the collider points like i added the triangles?
  3. is it possible to cut the collider that i have one part on the left side and one on the right, so that they are not connected to each other?

Yes you should use Polygon Collider. But if you want to have two separate colliders on one tile I think you will have to generate collider via script depending on the tile. Or it can be done in different way (I’m not sure how).