- Home /
Other
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.
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;
}
}
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();
}
Thank you.It was so easy to create and I didnˇt saw the solution. You sir have saved me.
And what if we were to add another axis to this say the Y for vertical neighbors?
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.
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
Related Questions
Random Room Generator 1 Answer
How to store Transform in an Transform array ? 2 Answers
Overlapping Tile Generation 0 Answers
Create a Pause In A For Loop 1 Answer