- Home /
How to make different objects spawn on a grid pattern randomly?
Hello, everyone. I would like to make a map for my game on which simple prefab pieces of terrain, all the same width and length, spawn randomly on a grid like pattern, like filling a 5x5 cm child's block puzzle with 25 different colored 1 cm cubes until the hole is perfectly filled. I can't even begin to think about how to do this. If anyone has the answer, I would be very grateful for their help. Thanks!
An array of the available prefab GameObjects.A nested loop that goes through the rows and columns of your grid and instantiates a random prefab from the array.
public GameObject[] prefabs = new GameObject[0];
public int gridSizeX = 5;
public int gridSizeY = 5;
void Start() {
for(int x = 0; x < gridSizeX; x++) {
for(int y = 0; y < gridSizeY; y++) {
GameObject go = Instantiate(prefabs[Random.Range(0,prefabs.Length), new Vector3(x,y,0), Quaternion.identity) as GameObject;
}
}
}
Assu$$anonymous$$g 1 unity unit is 1 cm. The prefabs array has to be filled in the inspector (or via code). The Vector3's y and z values need to be switched if the grid should be flat along the xz axes.
Just noticed that there was already a reply (using the same logic) ;)
@Cherno thanks, this works very well, but how would I increase the size of each "unit"?
Answer by Koyemsi · May 06, 2018 at 12:23 PM
Hello. There might be an approach using tilemaps if you're doing 2D. But my solution will be more basic. Here is what I did :
Created a few prefabs as map "tiles". Of course all of them have to be the exact same dimensions (at least their x and z dimensions).
Created an empty GO called @MapHolder. This will be the parent object for your map "tiles".
Assigned the RandomMap script to it
Filled the needed fields in its inspector.
Here is the script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomMap : MonoBehaviour {
[Tooltip ("Rows and cols for the map grid")]
public Vector2 myGrid;
[Tooltip ("Prefabs dimensions in Unity units")]
public Vector3 tileDimensions;
[Tooltip ("Populate with all the prefabs used to generate map")]
public GameObject[] prefabTiles;
// storing the map tiles in a list could be useful
List<GameObject> mapList = new List<GameObject> ();
// Use this for initialization
void Start () {
CreateMap ();
InvokeRepeating ("FunkyTiles", 0f, 0.05f);
}
void CreateMap () {
for (int row = 1; row <= myGrid.y; row++) {
for (int col = 1; col <= myGrid.x; col++) {
// choose a random prefab tile
int n = Random.Range (0, prefabTiles.Length);
GameObject thePrefab = prefabTiles [n];
// spawns the tile
GameObject theTile = Instantiate (thePrefab, transform);
theTile.name = "Tile_" + col + "_" + row;
theTile.transform.localPosition = new Vector3 ((col - 1) * tileDimensions.x, 0f, (row - 1) * tileDimensions.z);
// stores the tile in the List
mapList.Add (theTile);
}
}
print (mapList.Count + " tiles in the map");
}
// just for some extra fun ;)
void FunkyTiles () {
int n = Random.Range (0, mapList.Count);
GameObject theTile = mapList [n];
Vector3 temp = theTile.transform.localPosition;
temp.y = Random.Range (-0.25f, 0.25f);
theTile.transform.localPosition = temp;
}
}
Your answer
Follow this Question
Related Questions
2D Board game grid placement 0 Answers
Grid snapping 0 Answers
Grid Layout Group not working for 3d cube 0 Answers
How to create a rotating tiles with walls on edges of tiles? 0 Answers