I am trying to develop a small Game with a Hex Grid. I need to find the Neighbors of one Hex Tile in a specific radius.
Here is a screenshot of my Problem: Screenshot
I followed this awesome tutorial along and implemented some of the systems presented here: https://www.redblobgames.com/grids/hexagons/
public GameObject[] GetNeighbarsInRange(Vector2 pos, int range) {
List<GameObject> tiles = new List<GameObject>();
int min = -range, max = range;
for (int x = min; x <= max; x++) {
for(int y = min; y <= max; y++) {
for(int z = min; z <= max; z++) {
if (x + y + z == 0) {
Vector2 newPos = getVector2Coord(new Vector3(x, y, z)), newTilePos = new Vector2(pos.x + newPos.x, pos.y + newPos.y);
tiles.Add(getTile(newTilePos));
}
}
}
}
return tiles.ToArray();
}
public Vector2 getVector2Coord(Vector3 pos) {
int x = (int) pos.x, z = (int) pos.z;
int newX = x, newZ = z + (x - (x&1)) / 2;
return new Vector2(newX, newZ);
}
public Vector3 getVector3Coord(Vector2 pos) {
int x = (int) pos.x, y = (int) pos.y;
int tileX = x, tileZ = y - (x - (x&1)) / 2, tileY = -tileX - tileZ;
return new Vector3(tileX, tileY, tileZ);
}
Here you can see my Neighbor calculation and my Cube to Offset Coords and my Offset Coords to Cube Functions. My Grid is based on the Offset Coord System, I am saving my Grid in a 2D Array and when I search for neighbors I convert them into the Cube Format. My Problem is for the even cols its working fine but for the odd cols, it is not. My Question is how can I fix this?
It is btw. the same if I increase the radius to 2 instead of 1.
Kind regards,
Alex
(Sorry for my bad English I am from Germany:D)