Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
This question was closed Nov 20, 2020 at 02:58 PM by Rolo552 for the following reason:

Other

avatar image
0
Question by Rolo552 · Feb 07, 2020 at 03:47 PM · randomgenerationfor-loophexagonneighbour

How to get neighbor from for loop in hexagonal grid ?

Hello, I have a small problem, I do not know how to asign neighbours to an object.


What I want is to know which hexagons are neighbours of hexagon A. alt text

I know that it can be done through for loop, but I do not know how to create it so that object A knows that object B,C,D,E,F,G are neighbours of object A.

Here is my script for the hexagon map generation, hope it helps

 public class MapGenerator : MonoBehaviour
 {
     public int mapSize;
 
     public GameObject hexagonCellPrefab;
     public GameObject grassLand;
     public GameObject[] hexagonCells;
     public Vector3[] cellPositions;
 
     public int numberOfIslands;
 
     public bool worldIsGenerated;
     int i;
 
     Vector3 hexagonPosition;
     void Start()
     {
         GenerateMap();
     }
 
     void GenerateMap() 
     {
         hexagonCells = new GameObject[mapSize * mapSize];
         cellPositions = new Vector3[mapSize * mapSize];
 
         for (int x = 0,i = 0; x < mapSize; x++)
         {
             for (int z = 0; z < mapSize; z++)
             {
                 CreateHexagonCell(x, z, i++);
                 worldIsGenerated = true;            
             }
         }
 
         /* 
         if (worldIsGenerated)
         {
             for (int o = 0; o < numberOfIslands; o++)
             {
             
                 //Vygeneruj náhodné číslo mezi 0 a maximálním počtem hexagonů a vymaž je ze zásobníku
                 int randomNumber = Random.Range(o, hexagonCells.Length);
 
                 var randomObject = new GameObject[numberOfIslands];
                 randomObject[o] = hexagonCells[randomNumber];
 
                 hexagonCells[randomNumber] = hexagonCells[o];
                 hexagonCells[o] = randomObject[o];
  
             }
         }
         */
         
     }
     void CreateHexagonCell(int x,int z,int i) 
     {
         HexagonCell hexCellInfo = hexagonCellPrefab.GetComponent<HexagonCell>();
 
         hexagonPosition.x = (x + z * 0.5f - z/2) * (hexCellInfo.innerHexagonRadius * 2f);
         hexagonPosition.y = 0;
         hexagonPosition.z = z * (hexCellInfo.outerHexagonRadius * 1.5f);
 
         GameObject hexCell = hexagonCells[i] = Instantiate<GameObject>(hexagonCellPrefab) as GameObject;
         cellPositions[i] = hexagonPosition;
         hexCell.transform.SetParent(transform, false);
         hexCell.transform.localPosition = hexagonPosition;
 
     }
 }

vystrizek.png (19.3 kB)
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Feb 07, 2020 at 04:46 PM

 public GameObject[] GetNeighbours(int x, int z)
 {
     List<GameObject> neighbours = new List<GameObject>(6);
 
     // Left (G)
     if( x > 0 ) neighbours.Add(hexagonCells[x - 1 + z * mapSize]);
 
     // Right (D)
     if( x < mapSize ) neighbours.Add(hexagonCells[x + 1 + z * mapSize]);
 
     if(z % 2 == 0)
     {
         // Top Left (B)
         if( z > 0 ) neighbours.Add(hexagonCells[(x - 1) + (z - 1) * mapSize]);
 
         // Top Right (C)
         if( z > 0 ) neighbours.Add(hexagonCells[x + (z - 1) * mapSize]);
         
         // Bottom Left (F)
         if( z < mapSize ) neighbours.Add(hexagonCells[(x - 1) + (z + 1) * mapSize]);
 
         // Bottom Right (E)
         if( z < mapSize ) neighbours.Add(hexagonCells[x + (z + 1) * mapSize]);
     }
     else
     {
         // Top Left (B)
         if( z > 0 ) neighbours.Add(hexagonCells[x + (z - 1) * mapSize]);
 
         // Top Right (C)
         if( z > 0 ) neighbours.Add(hexagonCells[(x - 1) + (z - 1) * mapSize]);
         
         // Bottom Left (F)
         if( z < mapSize ) neighbours.Add(hexagonCells[x + (z + 1) * mapSize]);
 
         // Bottom Right (E)
         if( z < mapSize ) neighbours.Add(hexagonCells[(x - 1) + (z + 1) * mapSize]);
     }

     return neighbours.ToArray();
 }

Comment
Add comment · Show 2 · Share
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
avatar image Rolo552 · Feb 07, 2020 at 05:25 PM 0
Share

Thank you.It was so easy to create and I didnˇt saw the solution. You sir have saved me.

avatar image DJ_Design · Nov 18, 2021 at 02:46 AM 0
Share

And what if we were to add another axis to this say the Y for vertical neighbors?

avatar image
0

Answer by unity_ek98vnTRplGj8Q · Feb 07, 2020 at 04:06 PM

Trivial way to do it would be to add all hex cells to a list as you create them, then once they are all created you can loop through the list and check the distance to each cell and mark as a neighbor if that distance is small enough. Not the most efficient way but as long as you don't have thousands of cells it shouldn't be too bad.

 foreach (GameObject cell in cellList){
     foreach (GameObject otherCell in cellList)
     {
         if(cell != otherCell){
             if(Vector3.Distance(cell, otherCell) <= cellDistance){
                 //Add otherCell to cell's list of neighbors
             }
         }
     }
 }


NOTE- You should only calculate this ONCE at startup. I recommend adding a script to each cell that keeps track of it's neighbors, because recalculating at run time every time you need to find the neighbors will be very inefficient.

Comment
Add comment · Show 1 · Share
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
avatar image unity_ek98vnTRplGj8Q · Feb 07, 2020 at 04:10 PM 0
Share

If you want to know WHICH neighbor is which (upper right, left, etc.) you can just compare their directions. Also there are ways to do this that are much faster and more efficient, but unless you NEED that efficiency I wouldn't bother

Follow this Question

Answers Answers and Comments

128 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

Related Questions

Making an Array of Animations Play at Random With No Repeats 3 Answers

Help with Generating Random Tiles 1 Answer

Random Button / Plane Generation. 0 Answers

How to generate random terrain for a platformer 0 Answers

Random Tile Generation 0 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