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 *= Instantiate<GameObject>(hexagonCellPrefab) as GameObject;*
cellPositions = hexagonPosition;
hexCell.transform.SetParent(transform, false);
hexCell.transform.localPosition = hexagonPosition;
}
}