- Home /
Random hex grid
Hey, I'd like to be able to make hexagon prefabs spawn randomly but next to each other, to make some sort of grid. That way, each time the game starts, the player would get a different grid. I really don't know how to approach this.
Any suggestions would be much appreciated, thanks!
Does the grid shape change or does the contents of each hex square change? If the grid changes what is the shape. Is this grid going to be regular (i.e. some number of rows of the same count), random dense (every cell filled but wth a random shape), sparce (pathways of hex tiles), or something else?
ideally, it would not be a regular grid with a given number of row and they're would be different kinds of hexagons, but all the same shape.
A picture would be very helpful. It’s hard to suggest a good approach without a better idea of what you are trying to do because there are lots of ways to solve this problem. For example you could produce a “full page” of hexagons and then, using whatever algorithm you wanted, change the texture for different looks or turn off the renderer and/or disable game objects to eli$$anonymous$$ate hexagons from the grid. You wouldn't be creating and destroying game object at runtime so there would never be any duplicates. Or you can start with a single hexagon and randomly walk outward creating objects as you go. To eli$$anonymous$$ate duplicates with this approach, keep an array of all the already created hexagons (or empty game objects if that is what you are using as placeholders). You can then compare the distance between all of the already placed hexagons and a proposed new hexagon. If the distance is below some small threshold, then the hexagon already exists. If you are going to be changing the grid frequently or had a very large number of hexagons, I might suggest yet another approach.
Answer by harko12 · Jan 13, 2013 at 06:46 AM
well, one thing you could do is make your prefabs, then spawn one, and do a random number between 1-6. Then add another hex to that side of the one you have. then random again, and add one to the new hex, and so forth. If you get a side that's already taken with the random number, then pick a different side, etc.
Pretty basic but it might get you going.
Thanks, I'll look into that! Although, how would you check if a hexagon is already on one of the sides? What I did is I used empty GameObjects as spawn points, and each time a number is randomly picked, a hexagon (with spawns attached to him as well) spawns at any of the spawns with the right position and rotation. But the problem now is the hexagons overlaying each other...
After I gave the answer, I decided to try it out. I ran into the same problem. The simplest way would probably be to test if the spawn point overlaps an existing tile before spawning one. I had made a script for each tile that keeps track of it's neighbors on each side, but that may be overkill. If I get it working, I'll post the code
Sounds good, I'm trying to give the spawn points colliders or/and triggers to destroy themselves before a new tile is spawned, but OnCollisionEnter and OnTriggerEnter don't seem to be working in this case
Im also trying with Vector3.distance, but its not giving me the desired results either
Prior to the random number generation, you cast normal rays from all the sides to check which ones are actually empty.
The Ray idea should work as long as the ray length is short enough not to see a tile more than one space away. probably simpler than a collider. The trick there will be getting the normal that you need. There may be an easy way to do that, but I'm not sure what it is.
Answer by Chronos-L · Jan 13, 2013 at 07:13 AM
If you are looking for the same map, but different arrangements of grid, I will propose that you should manually make a generic map prefab (with a bunch of empty objects placed in a hexagonal patterns). Then on game start, you will randomly place different hexagons grid on each of these locations:
public Hexagon[] hexs; //Manully assigned
public Transform[] arrangement; //Manually assigned
private List <Hexagon> tiles = new List<Hexagon>();
void Start() {
for( int i = 0; i < arrangement.Length; ++i ) {
Hexagon newHex = Instantiate( hexs[ Random.Range(0, hexs.length) ], arrangement[i].position, arrangement[i].rotation);
tiles.Add(newHex); // This is optionally
}
}
Side note:
Assuming you have 1 blue tile, 1 red tile, and 1 green tile. And you need their ratio to be 7:2:1; in the inspector, you will put in 8 blue tile prefabs, 1 red and 1 green in the hexs variable.
For arrangement, you can either do it programmatically(can adapt to different map) or manually(cheap & quick solution)
Thanks, that's great, but I wouldn't want the same map everytime
You can insert a null into the hexs
for it to insert nothing as parts of the map, so you will get holes/missing tiles/blank tiles. But I'glad that you got your answer from harko12, I am not exactly sure of what you want, so I just provide a solution from what I can think of when I think about a random hex-grid map.