So, I've set up a script that finds a players position relative to the chunks that make up my world. I was wondering If there was a way to get from a center point, and load outwards from the player - something like layers.
Code for those who are wondering:
using UnityEngine;
using System.Collections;
[System.Serializable()]
public class LoadChunks : MonoBehaviour {
public Planet planet;
public bool finished = true;
public float planetx;
public float planety;
public float planetz;
public float x;
public float y;
public float z;
float lastx;
float lasty;
float lastz;
public float length;
public float Range = 32;
// Update is called once per frame
public void Update () {
if (finished == true)
{
if (planet == null)
{
GetPlanet();
Vector3 pos = planet.transform.position;
planetx = pos.x;
planety = pos.y;
planetz = pos.z;
length = planet.planetSize / 16;
}
//Getting player position
x = this.transform.position.x;
y = this.transform.position.y;
z = this.transform.position.z;
//Check if the player has moved, skip a few calculations if they didn't
if (x != lastx || y != lasty || z != lastz)
{
//Normalizing position with planet position
x = x - planetx;
y = y - planety;
z = z - planetz;
//Converts into chunk position
x = Mathf.FloorToInt(x / (float)localVars.chunklength);
y = Mathf.FloorToInt(y / (float)localVars.chunklength);
z = Mathf.FloorToInt(z / (float)localVars.chunklength);
}
//Make sure the player is within the chunks ranges
if (x > -Range && y > -Range && z > -Range && x <= length + Range && y <= length + Range && z <= length + Range)
{
LoadChunksInRadius(x, y, z);
lastx = x;
lasty = y;
lastz = z;
}
}
}
void LoadChunksInRadius(float x, float y, float z)
{
}
void GetPlanet()
{
//TODO: Make this get closest planet once multi-planet functionality is added
planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<Planet>();
}
}