- Home /
Question by
JulianMagicCraft · Mar 14 at 10:23 AM ·
2d2d gameblockssandboxworld coordinates
so i have a script which generates a 2d block world but how do i make a block placeing script which matches the blocks with my generated world?
The World generaor Script: public int width; public int height; public int distance; public int space;
public GameObject Grass;
public GameObject Dirt;
public GameObject Stone;
public float heightpoint;
public float heightpoint2;
// Use this for initialization
void Start()
{
Generation();
}
void Generation()
{
distance = height;
for (int w = 0; w < width; w++)
{
int lowernum = distance - 1;
int heighernum = distance + 2;
distance = Random.Range(lowernum, heighernum);
space = Random.Range(12, 20);
int stonespace = distance - space;
for (int j = 0; j < stonespace; j++)
{
Instantiate(Stone, new Vector3(w, j), Quaternion.identity);
}
for (int j = stonespace; j < distance; j++)
{
Instantiate(Dirt, new Vector3(w, j), Quaternion.identity);
}
Instantiate(Grass, new Vector3(w, distance), Quaternion.identity);
}
},the generating script:
public int width;
public int height;
public int distance;
public int space;
public GameObject Grass;
public GameObject Dirt;
public GameObject Stone;
public float heightpoint;
public float heightpoint2;
// Use this for initialization
void Start()
{
Generation();
}
void Generation()
{
distance = height;
for (int w = 0; w < width; w++)
{
int lowernum = distance - 1;
int heighernum = distance + 2;
distance = Random.Range(lowernum, heighernum);
space = Random.Range(12, 20);
int stonespace = distance - space;
for (int j = 0; j < stonespace; j++)
{
Instantiate(Stone, new Vector3(w, j), Quaternion.identity);
}
for (int j = stonespace; j < distance; j++)
{
Instantiate(Dirt, new Vector3(w, j), Quaternion.identity);
}
Instantiate(Grass, new Vector3(w, distance), Quaternion.identity);
}
}
Comment
Answer by rh_galaxy · Mar 15 at 10:32 PM
If I understand it correctly you generate a world with stones in a grid. And after that you want to place blocks on some places in the world that does not contain a stone? At least that is how I understand your task.
I suggest that you keep a copy of the grid so you can remember where the stones are, and then just place on empty positions.
const int MAXWIDTH = 30;
const int MAXHEIGHT = 30;
bool[,] aMap;
void Generation()
{
aMap = new int[MAXWIDTH, MAXHEIGHT];
for(int w = 0; w < MAXWIDTH; w++)
for(int h = 0; h < MAXHEIGHT; h++)
aMap[w, h] = false;
//...
for (int w = 0; w < width; w++)
{
//...
for (int j = stonespace; j < distance; j++)
{
aMap[w, j] = true; //block placement possible (on dirt)
}
}
}
public int numBlocks = 5;
void PlaceBlocks()
{
int i = 0;
while(i < numBlocks)
{
int w = Random.Range(0, MAXWIDTH);
int h = Random.Range(0, MAXHEIGHT;
if(aMap[w, h])
{
//TODO place block here
i++;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613054135im_/https://answers.unity.com/themes/thub/images/avi.jpg)