I’m trying to have it so only the terrain tiles close to the player are active to cut on resource usage and so my computer doesn’t crash. For some reason, my code doesn’t work 100% as intended. I start with this, character at selescted
and end up with this, with these distance values
I don’t understand why it deactivated the tile the player stands on even though it recorded the distance correctly and it is under the required amount?
This is the code being used
public class HideTiles : MonoBehaviour
{
[SerializeField]
private string tileTag;
[SerializeField]
private Vector3 tileSize;
[SerializeField]
private int maxDistance;
private GameObject[ ] tiles;
public float[ ] dist = new float[16];
// Use this for initialization
void Start()
{
this.tiles = GameObject.FindGameObjectsWithTag(tileTag);
DeactivateDistantTiles();
}
void DeactivateDistantTiles()
{
Vector3 playerPosition = this.gameObject.transform.position;
int num = 0;
foreach (GameObject tile in tiles)
{
Vector3 tilePosition = tile.gameObject.transform.position; //+ (tileSize / 2f);
float xDistance = Mathf.Abs(tilePosition.x - playerPosition.x);
float zDistance = Mathf.Abs(tilePosition.z - playerPosition.z);
float distance = Mathf.Sqrt((xDistance * xDistance) + (zDistance * zDistance));
dist[num] = distance; //getting the distance into the inspector
num += 1; //
if (distance > maxDistance)
{
tile.SetActive(false);
}
else
{
tile.SetActive(true);
}
}
}
void Update()
{
DeactivateDistantTiles();
}
}
What’s wrong here?
Using unity 2019.3.11