• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PopRockLex · Jan 03 at 11:24 PM · 3dhexagon

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't generate 3D meshes via script 1 Answer

Unity Probuilder mesh brush and grids. 0 Answers

Same Collision Rebound Force on Cars 0 Answers

Camera Keeps Following Mouse in Pause Menu 0 Answers

Want to make GameObject follow another GameObject's path Unity3D 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges