I’m trying to make a procedural world around my player, but it doesn’t go right when you press update it spawns them in a line so rows is working, it seems to not want to make the next line of modes while the player is moving around, anyone got any ideas?
class ThisTile
{
public GameObject theTile;
public float creationTime;
public ThisTile(GameObject t, float ct)
{
theTile = t;
creationTime = ct;
}
}
public Texture2D tex;
public Material mat;
public GameObject player, quad;
Vector3 startPos;
Hashtable tiles = new Hashtable();
public int cols = 6;
void Start()
{
this.gameObject.transform.position = Vector3.zero;
startPos = Vector3.zero;
player.transform.position = startPos;
float updateTime = Time.realtimeSinceStartup;
int rows = Mathf.RoundToInt(cols);
Vector3 offset = Vector3.zero;
offset.x = -Mathf.RoundToInt((float)cols / 2.0f - 0.5f);
offset.z = -Mathf.RoundToInt((float)rows / 2.0f - 0.5f);
float startX = offset.x;
float uvWidth = 1.0f / cols;
float uvHeight = 1.0f / rows;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
GameObject go = (GameObject)Instantiate(quad);
Transform t = go.transform;
t.position = offset;
t.localScale = new Vector3(1.0f, 1.0f, 1.0f);
t.transform.rotation = Quaternion.Euler(90, 0, 0);
mat.mainTexture = tex;
go.GetComponent<Renderer>().material = mat;
string tileName = "Tile_" + ((float)(go.transform.position.x)).ToString() + "_" + ((float)(go.transform.position.z)).ToString();
go.name = tileName;
ThisTile tile = new ThisTile(go, updateTime);
tiles.Add(tileName, tile);
go.transform.SetParent(this.transform);
// Displays the texture across several quads
Mesh mesh = go.GetComponent<MeshFilter>().mesh;
Vector2[] uvs = mesh.uv;
uvs[0] = new Vector2(j * uvWidth, i * uvHeight);
uvs[3] = new Vector2(j * uvWidth, (i + 1) * uvHeight);
uvs[1] = new Vector2((j + 1) * uvWidth, (i + 1) * uvHeight);
uvs[2] = new Vector2((j + 1) * uvWidth, i * uvHeight);
mesh.uv = uvs;
offset.x += 1.0f;
}
offset.z += 1.0f;
offset.x = startX;
}
}
void Update()
{
float xMove = (float)(player.transform.position.x - startPos.x);
float zMove = (float)(player.transform.position.z - startPos.z);
int rows = Mathf.RoundToInt(cols);
Vector3 offset = Vector3.zero;
offset.x = -Mathf.RoundToInt((float)cols / 2.0f - 0.5f);
offset.z = -Mathf.RoundToInt((float)rows / 2.0f - 0.5f);
float startX = offset.x;
if (Mathf.Abs(xMove) >= 1.0f || Mathf.Abs(zMove) >= 1.0f)
{
float updatetime = Time.realtimeSinceStartup;
int playerX = (int)(Mathf.Floor(player.transform.position.x / 1.0f) * 1.0f);
int playerZ = (int)(Mathf.Floor(player.transform.position.z / 1.0f) * 1.0f);
for (float i = 0; i < rows; i++)
{
for (float j = 0; j < cols; j++)
{
Vector3 pos = new Vector3((i * 1.0f + playerX), 0, (j * 1.0f + playerZ));
string tileName = "Tile_" + ((float)(pos.x)).ToString() + "_" + ((float)(pos.z)).ToString();
if (!tiles.ContainsKey(tileName))
{
// print("Obj exists");
GameObject t = (GameObject)Instantiate(quad);
t.transform.SetParent(this.transform);
t.name = tileName;
t.transform.position = offset ;
t.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
t.transform.rotation = Quaternion.Euler(90, 0, 0);
ThisTile tile = new ThisTile(t, updatetime);
tiles.Add(tileName, tile);
}
else
{
(tiles[tileName] as ThisTile).creationTime = updatetime;
}
}
offset.x += 1.0f;
}
offset.z += 1.0f;
offset.x = startX;
Hashtable newTile = new Hashtable();
foreach (ThisTile tls in tiles.Values)
{
if (tls.creationTime != updatetime)
{
Destroy(tls.theTile);
}
else
{
newTile.Add(tls.theTile.name, tls);
}
}
tiles = newTile;
startPos = player.transform.position;
}
}