Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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, 2021 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.

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

Enemy should follow the Player if he is in range, but how? 2 Answers

Can I export a level from Unity to Maya ? 1 Answer

WaitForSecond Corountine Sleep Time Varies 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges