i am working on a game using a 2d chunk loading system i have been working on, and i tried to convert it to tiles, but the tiles remain at the position (0,0). Anyone know how to fix this?
code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class generation : MonoBehaviour
{
[SerializeField] int width;
[SerializeField] GameObject Player,Grass,Dirt,Stone,Coal,Wood;
[SerializeField] Tile grass,dirt,stone,coal,wood;
[SerializeField] Tilemap genchunk;
[Range(0,500)]
[SerializeField] float heightValue, smoothness;
[SerializeField] float modifier,seed,treeModifier,coalModifier;
// Start is called before the first frame update
void Start()
{
seed = Random.Range(-1000000,1000000);
chunkgen();
Pgeneration((uint)0);
}
public Grid grid;
public Tilemap[] worldchunks;
// Update is called once per frame
public void ClearChildren()
{
int i = 0;
//Array to hold all child obj
GameObject[] allChildren = new GameObject[transform.childCount];
//Find all child obj and store to that array
foreach (Transform child in transform)
{
allChildren[i] = child.gameObject;
i += 1;
}
//Now destroy them
foreach (GameObject child in allChildren)
{
Transform e = child.gameObject.GetComponent<Transform>();
int x1 = Mathf.RoundToInt(e.position.x);
e = Player.gameObject.GetComponent<Transform>();
int x2 = Mathf.RoundToInt(e.position.x);
if(-Mathf.Abs(x1)+Mathf.Abs(x2) > 64)
{
for (i = 0; i < (x1/16+6); i++)
{
if(worldchunks[(x1/16+7)] == null)
{
worldchunks[(x1/16)] = null;
Destroy(child.gameObject);
generateC((x1/16+7)*16);
}
if(worldchunks[(x1/16+1)] == null)
{
worldchunks[(x1/16)] = null;
Destroy(child.gameObject);
generateC((x1/16+9)*16);
}
}
}
}
}
void generateC(int x)
{
//worldchunks = new Tilemap[Mathf.RoundToInt(x/16)];
Tilemap newChunk = Instantiate(genchunk, new Vector2(0,0), Quaternion.identity);
newChunk.name = Mathf.Round(x/16).ToString();
newChunk.transform.parent = this.transform;
worldchunks[x/16] = newChunk;
Transform cpos = newChunk.gameObject.GetComponent<Transform>();
cpos.position = new Vector2(x,cpos.position.y);
Pgeneration((uint)x);
}
void chunkgen()
{
int chunks = width/16;
worldchunks = new Tilemap[1000000];
for (int i = 0;i < chunks;i++)
{
Tilemap newChunk = Instantiate(genchunk, new Vector2(0,0), Quaternion.identity);
newChunk.name = i.ToString();
newChunk.transform.parent = this.transform;
worldchunks[i] = newChunk;
Transform cpos = newChunk.gameObject.GetComponent<Transform>();
//cpos.position = new Vector2((i+1)*16,cpos.position.y);
}
}
void Update()
{
ClearChildren();
//Pgeneration();
if(Input.GetMouseButtonDown(1))
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int position = grid.WorldToCell(mousePos);
}
}
void Pgeneration(uint xoffset)
{
int lastgen = 0;
int lastgencoal = 0;
for (uint x = 0 + xoffset; x < width + xoffset; x++)
{
int height = Mathf.RoundToInt (heightValue * Mathf.PerlinNoise(x / smoothness, seed*63));
int totalStoneSpawn = height - Mathf.RoundToInt(Mathf.PerlinNoise(x,(height*modifier)+seed));
for (uint y = 0; y < height; y++)
{
if (Mathf.RoundToInt(y)<totalStoneSpawn-5)
{
int minCoalChance = 0;
int maxCoalChance = 100;
int coalrange = Mathf.RoundToInt(x)+Mathf.RoundToInt(Mathf.PerlinNoise((lastgencoal*coalModifier)+seed,(Mathf.RoundToInt(y)*coalModifier)+seed));
int coalgen = Mathf.RoundToInt(x) - Mathf.RoundToInt(Mathf.PerlinNoise((x*coalModifier)+seed,(Mathf.RoundToInt(y)*coalModifier)+seed));
int coalValue = Random.Range(minCoalChance,maxCoalChance);
if (97 < coalValue)
{
if (Mathf.RoundToInt(y)<50000)
{
spawnOBJ(coal,Mathf.RoundToInt(x),Mathf.RoundToInt(y));
}
else
{
spawnOBJ(stone,Mathf.RoundToInt(x),Mathf.RoundToInt(y));
}
}
else
{
spawnOBJ(stone,Mathf.RoundToInt(x),Mathf.RoundToInt(y));
}
}
else
{
spawnOBJ(dirt,Mathf.RoundToInt(x),Mathf.RoundToInt(y));
}
}
spawnOBJ(grass,Mathf.RoundToInt(x),height);
float ypos = height;
int range = Mathf.RoundToInt(x-xoffset) + Mathf.RoundToInt(Mathf.PerlinNoise((lastgen*modifier)+seed,ypos));
int treegen = Mathf.RoundToInt(x-xoffset) - Mathf.RoundToInt(Mathf.PerlinNoise((Mathf.RoundToInt(x)*treeModifier)+seed,ypos));
if (range>(lastgen+treegen)/1.99)
{
int tree = Mathf.RoundToInt(Mathf.PerlinNoise(Mathf.RoundToInt(x-xoffset),modifier*seed));
for (int add = 1;add<tree+4;add++)
{
spawnOBJ(wood,Mathf.RoundToInt(x),height+add);
}
lastgen = lastgen+5;
}
}
}
void spawnOBJ(Tile obj,int width,int height)
{
int chunkCoord = (Mathf.RoundToInt(width/16)* 16);
chunkCoord /= 16;
//obj = Instantiate(obj, new Vector2(width,height), Quaternion.identity);
//obj.transform.parent = worldchunks[chunkCoord].transform;
worldchunks[chunkCoord].SetTile(new Vector3Int(width,height,0),obj);
}
}