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.
for now that worked 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
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).