So I'm working on a voxel game with cubic chunks. A chunk is 4m cube. I want to get a list of all the coordinates in which to spawn these chunks around the player as a sphere, to simulate a render distance. Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChunkManager : MonoBehaviour
{
public List<ChunkCoordinate> chunkSlots = new List<ChunkCoordinate>();
public ChunkCoordinate chunkSlotInsert = new ChunkCoordinate();
public float updateCooldown;
public int chunkSlotIndex;
public int chunkSlotIndexX;
public int chunkSlotIndexY;
public int chunkSlotIndexZ;
public bool chunkSlotUpdated;
public int renderDistance;
static public bool requestRDU;
void Start(){
requestRDU = true;
updateCooldown = 1;
renderDistance = 50;
}
void Update()
{
//Chunk Updator
updateCooldown -= Time.deltaTime;
if(updateCooldown < 0){
updateCooldown = 1;
if(requestRDU){UpdateRenderDistance();}
}
}
public void UpdateRenderDistance(){
chunkSlotUpdated = false;
chunkSlotIndexX = 0;
chunkSlotIndexY = 0;
chunkSlotIndexZ = 0;
chunkSlotIndex = 0;
//Generate an eighth of the chunk slot sphere
chunkSlotInsert.x = 0;
chunkSlotInsert.y = 0;
chunkSlotInsert.z = 0;
chunkSlots.Add(chunkSlotInsert);
Debug.Log(chunkSlotInsert.x.ToString() +", "+ chunkSlotInsert.y.ToString() +", "+ chunkSlotInsert.z.ToString());
while(!chunkSlotUpdated){
chunkSlotInsert.x += 1;
if(Vector3.Distance(new Vector3(chunkSlotInsert.x, chunkSlotInsert.y, chunkSlotInsert.z), new Vector3(0, 0, 0)) > renderDistance){
chunkSlotInsert.x = 0;
chunkSlotInsert.y += 1;
if(Vector3.Distance(new Vector3(chunkSlotInsert.x, chunkSlotInsert.y, chunkSlotInsert.z), new Vector3(0, 0, 0)) > renderDistance){
chunkSlotInsert.x = 0;
chunkSlotInsert.y = 0;
chunkSlotInsert.z += 1;
if(Vector3.Distance(new Vector3(chunkSlotInsert.x, chunkSlotInsert.y, chunkSlotInsert.z), new Vector3(0, 0, 0)) > renderDistance){
chunkSlotUpdated = true;
}
else{
chunkSlots.Add(chunkSlotInsert);
}
}
else{
chunkSlots.Add(chunkSlotInsert);
}
}
else{
chunkSlots.Add(chunkSlotInsert);
}
Debug.Log(chunkSlotInsert.x.ToString() +", "+ chunkSlotInsert.y.ToString() +", "+ chunkSlotInsert.z.ToString());
}
//Duplicate chunk slot info on x axis
chunkSlotIndex = 0;
while(chunkSlots.Count > chunkSlotIndex){
Debug.Log(chunkSlots[chunkSlotIndex].x.ToString() +", "+ chunkSlots[chunkSlotIndex].y +", "+ chunkSlots[chunkSlotIndex].z);
chunkSlotInsert.x = 0 - (chunkSlots[chunkSlotIndex].x + 1);
chunkSlotInsert.y = chunkSlots[chunkSlotIndex].y;
chunkSlotInsert.z = chunkSlots[chunkSlotIndex].z;
if(!chunkSlots.Contains(chunkSlotInsert)){chunkSlots.Add(chunkSlotInsert);}
chunkSlotIndex += 1;
}
//Duplicate chunk slot info on y axis
chunkSlotIndex = 0;
while(chunkSlots.Count > chunkSlotIndex){
chunkSlotInsert.x = chunkSlots[chunkSlotIndex].x;
chunkSlotInsert.y = 0 - chunkSlots[chunkSlotIndex].y;
chunkSlotInsert.z = chunkSlots[chunkSlotIndex].z;
if(!chunkSlots.Contains(chunkSlotInsert)){chunkSlots.Add(chunkSlotInsert);}
chunkSlotIndex += 1;
}
//Duplicate chunk slot info on z axis
chunkSlotIndex = 0;
while(chunkSlots.Count > chunkSlotIndex){
chunkSlotInsert.x = chunkSlots[chunkSlotIndex].x;
chunkSlotInsert.y = chunkSlots[chunkSlotIndex].y;
chunkSlotInsert.z = 0 - chunkSlots[chunkSlotIndex].z;
if(!chunkSlots.Contains(chunkSlotInsert)){chunkSlots.Add(chunkSlotInsert);}
chunkSlotIndex += 1;
}
requestRDU = false;
}
}
[System.Serializable]
public class ChunkCoordinate{
public int x;
public int y;
public int z;
}
Now as you can see, I'm debugging all of the coordinates for an eighth of the spehere, and it works just fine. However, inside the list I serialized for ease of use, it gives nothing but the last coordinate. I don't know why that happens, it's not like I didn't just debug the proper coordinate. Is it something with how Unity or C# handles List.Add()?
Either way, thanks in advance. If you need more info, I'll be glad to share. <3