- Home /
How do I make sure my Hex based grid has hexes that touch each other?
I'm trying to make a game with a hex map, right now I have the hex system working. I'm trying to make it less boring and create a more random appearance. When I have "Spotty" on it should make it skip some hexes when it's creating the grid so it doesn't look like one big chunk. Right now it leaves these islands and I need it all to connect to each other. I've been stuck here for days. If anyone could point me in the right direction it would be greatly appreciated! Below is the Grid Generation part of my script and screenshots of what it's doing.
public void GenerateGrid()
{
var hexagons = new List<Cell>();
if (PlayerPrefs.GetInt("Spotty") == 0)
{
for (int i = 0; i < Radius; i++)
{
for (int j = 0; j < (Radius * 2) - i - 1; j++)
{
int maybeKill = UnityEngine.Random.Range(0, 2);
if (maybeKill == 1)
{
GameObject hexagon = PrefabUtility.InstantiatePrefab(HexagonPrefab) as GameObject;
var hexSize = hexagon.GetComponent<Cell>().GetCellDimensions();
hexagon.transform.position = new Vector3((i * hexSize.x * 0.75f), (i * hexSize.y * 0.5f) + (j * hexSize.y), 0);
hexagon.GetComponent<Hexagon>().OffsetCoord = new Vector2(i, Radius - j - 1 - (i / 2));
hexagon.GetComponent<Hexagon>().HexGridType = HexGridType.odd_q;
hexagon.GetComponent<Hexagon>().MovementCost = 1;
hexagons.Add(hexagon.GetComponent<Cell>());
hexagon.transform.parent = thisCellGrid.transform;
if (i == 0) continue;
GameObject hexagon2 = PrefabUtility.InstantiatePrefab(HexagonPrefab) as GameObject;
hexagon2.transform.position = new Vector3((-i * hexSize.x * 0.75f), (i * hexSize.y * 0.5f) + (j * hexSize.y), 0);
hexagon2.GetComponent<Hexagon>().OffsetCoord = new Vector2(-i, Radius - j - 1 - (i / 2));
hexagon2.GetComponent<Hexagon>().HexGridType = HexGridType.odd_q;
hexagon2.GetComponent<Hexagon>().MovementCost = 1;
hexagons.Add(hexagon2.GetComponent<Cell>());
hexagon2.transform.parent = thisCellGrid.transform;
}
}
}
var hexDimensions = HexagonPrefab.GetComponent<Cell>().GetCellDimensions();
gridInfo = new GridInfo();
gridInfo.Cells = hexagons;
gridInfo.Dimensions = new Vector3(hexDimensions.x * (Radius * 2) - 2, hexDimensions.y * ((Radius * 2) - 2), hexDimensions.z);
gridInfo.Center = new Vector3(0, gridInfo.Dimensions.y / 2, 0);
}
else if (PlayerPrefs.GetInt("Spotty") == 1)
{
for (int i = 0; i < Radius; i++)
{
for (int j = 0; j < (Radius * 2) - i - 1; j++)
{
GameObject hexagon = PrefabUtility.InstantiatePrefab(HexagonPrefab) as GameObject;
var hexSize = hexagon.GetComponent<Cell>().GetCellDimensions();
hexagon.transform.position = new Vector3((i * hexSize.x * 0.75f), (i * hexSize.y * 0.5f) + (j * hexSize.y), 0);
hexagon.GetComponent<Hexagon>().OffsetCoord = new Vector2(i, Radius - j - 1 - (i / 2));
hexagon.GetComponent<Hexagon>().HexGridType = HexGridType.odd_q;
hexagon.GetComponent<Hexagon>().MovementCost = 1;
hexagons.Add(hexagon.GetComponent<Cell>());
hexagon.transform.parent = thisCellGrid.transform;
if (i == 0) continue;
GameObject hexagon2 = PrefabUtility.InstantiatePrefab(HexagonPrefab) as GameObject;
hexagon2.transform.position = new Vector3((-i * hexSize.x * 0.75f), (i * hexSize.y * 0.5f) + (j * hexSize.y), 0);
hexagon2.GetComponent<Hexagon>().OffsetCoord = new Vector2(-i, Radius - j - 1 - (i / 2));
hexagon2.GetComponent<Hexagon>().HexGridType = HexGridType.odd_q;
hexagon2.GetComponent<Hexagon>().MovementCost = 1;
hexagons.Add(hexagon2.GetComponent<Cell>());
hexagon2.transform.parent = thisCellGrid.transform;
}
}
var hexDimensions = HexagonPrefab.GetComponent<Cell>().GetCellDimensions();
gridInfo = new GridInfo();
gridInfo.Cells = hexagons;
gridInfo.Dimensions = new Vector3(hexDimensions.x * (Radius * 2) - 2, hexDimensions.y * ((Radius * 2) - 2), hexDimensions.z);
gridInfo.Center = new Vector3(0, gridInfo.Dimensions.y / 2, 0);
}
}
Normal - https://i.stack.imgur.com/XIJV2.jpg Spotty - https://i.stack.imgur.com/oeC4j.jpg
Your answer

Follow this Question
Related Questions
Unity Probuilder mesh brush and grids. 0 Answers
Can't generate 3D meshes via script 1 Answer
How to make it real 3D? 1 Answer
weapon aiming in 3d space using the (2d) mouse 1 Answer
Need Help Getting Rope To Wrap/Straighten In 3D Space 0 Answers