- Home /
How do i spawn a square in random position within an area but only at certain x and y intervals?
basically its a randomly generated topdown view tile map but i want the tiles to spawn at intervals of 0.64 so they dont overlap.
Heres my code: public class newMapGen : MonoBehaviour { public int tileTypes;
public int maxX = 64;
public int maxY = 64;
bool runLoop = true;
public int tilesss = 0;
public System.Collections.Generic.List<GameObject> tiles = new System.Collections.Generic.List<GameObject>();
private GameObject Grass;
private GameObject Dirt;
private GameObject Rock;
private GameObject Gold;
void Start()
{
{
while (tilesss < 4096)
{
GameObject tileInstance;
tileInstance = Instantiate(tiles[Random.Range(0, tileTypes)]) as GameObject;
tileInstance.transform.localPosition = new Vector3(Random.Range(0f, 64f) * 0.64f, Random.Range(0f, 64f) * 0.64f, 0);
tileInstance.transform.parent = transform;
tilesss += 1;
}
}
}
}
Any help is appreciated :)
You didn't actually ask a question, you just stated what you are trying to do and pasted your code. What is wrong or incomplete with it?
I'll point out, though, that you should always set the parent transform BEFORE you set a LocalPosition, otherwise it'll be set relative to the old parent, which is not intended. That means you should switch those lines, making the parent be set first.
Other than that, you never set your list up, and that's probably what you're perceiving as the problem. Try adding this directly above your "while" statement:
tiles = new System.Collections.Generic.List<GameObject>() { Grass, Dirt, Rock, Gold };
Hello sorry for not specifying my problem clearly, currently the tiles spawn totally randomly, overlapping. i want them to only spawn once every 0.64 units so they do not overlap. the list seems to work, as the tiles spawn but i will definitely try what you said.
I only later realised you are setting the list from the inspector, and the 4 gameobjects are just uninitialized private variables.
Regarding their initial position, try using the int version of the Random.Range (by removing the "f" from the $$anonymous$$ and max)
Answer by Ramlock · Aug 30, 2018 at 10:09 AM
The blank spaces are because you're positioning the tiles randomly, which I assumed was intended. If you want every tile in the grid to be filled, you should do a double loop instead of random. Try this code:
void Start()
{
for(int x = 0; x < 64; x++)
{
for(int y = 0; y < 64; y++)
{
referenceTiles = Instantiate(tiles[Random.Range(0, centerPointVariety)]) as GameObject;
referenceTiles.transform.parent = transform;
referenceTiles.transform.localPosition = new Vector3(x * 0.64f, y * 0.64f, 0);
}
}
}
public class mapGeneration : $$anonymous$$onoBehaviour { public int x_length; public int y_length;
public int maxTileRange;
private int noOfTiles;
public int maxTiles;
public List<GameObject> tiles = new List<GameObject>();
GameObject referenceTiles = GameObject.FindWithTag("tileTag");
void Start()
{
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 64; y++)
{
if (noOfTiles < maxTiles)
{
referenceTiles = Instantiate(tiles[Random.Range(0, maxTileRange)]) as GameObject;
referenceTiles.transform.parent = transform;
referenceTiles.transform.localPosition = new Vector3(x * 0.64f, y * 0.64f, 0);
noOfTiles += 1;
}
}
}
}
}
this is what i have come up with, it does almost what i want. the only thing is it spawns the tiles in a straight line. i want them spawned randomly within the boundary so that i can use them as parent blocks and procedurally generate more terrain around them based on nearby tile types. Sorry if im confusing, trying my best to explain.
Can you provide a visual aid of what you're trying to achieve?