help with hexagon offset

Hello fellow coders :slight_smile:
please forgive me if this has already been asked before :slight_smile:
i´m working on a FOW feature. i already have a solid grid system going on for placing buildings etc. i´m trying to utilize the same 2d grid system with a bunch of hexagons.
So far i can spawn my hexagons in the correct pattern, however when i try to use one of my methods to convert vector 3 into a tile position. i get weird offset in my y direction (z direction in world space).
here´s some code snippets, my script is very big so no need to show all the code :wink:

this method gets called in a nested loop to make up a grid.

public void createHexTiles(Tile tilepos)
    {
        Vector3 position;
        float outerRadius = 6f;
        float innerRadius = outerRadius * 0.866025404f;

        //    position.x = (tilepos.x + tilepos.y * 0.5f - tilepos.y/2) * innerRadius * 2f ;
        position.y = 100f;
        //    position.z = tilepos.y * outerRadius * 1.5f ;

        //    position.x = x - offsetX;
        //    position.z = y  - offsetY;


        //    position = position - new Vector3(520,0,30);
//        position = new Vector3(tilepos.x*10,100f,tilepos.y*10);
            position = new Vector3 (0, 25, 0);

        GameObject fowReveal = new GameObject ();
        //    fowReveal.transform.position = position;
        fowReveal.transform.up = Vector3.forward;

        float offset = 0;
        if(tilepos.y % 2 !=0)
        {
            offset = 10 / 2;
        }

            position.x = position.x + tilepos.x * 10 + offset;
            position.z = position.z + tilepos.y * 10 * 0.75f;

        fowReveal.transform.position = position;
        //    fowReveal.transform.Rotate (new Vector3(0,0,30));

        fowReveal.transform.localScale = new Vector3 (20, 20, 20);
        fowReveal.AddComponent <SpriteRenderer> ().sprite = Reveal;
        fowReveal.layer = 11;
        fowReveal.name = "Holla";
        //    FogRevealList.Add (fowReveal.transform);


        tilepos.check_Fowchange ( (Tile) => {HexFowChanged (Tile,fowReveal);} );

    }

this code converts a vector 3 into a tile position and returns the tile.

public Tile getHexAtCoord(Vector3 Hexcoord)
    {
        int x = Mathf.FloorToInt (Hexcoord.x)/10;
        int y = Mathf.FloorToInt (Hexcoord.z)/10;
        int col = x;
        int row = y + (x - (x % 2)) / 2 ;
        print (y +"/"+row);
        return manager.GetTileAt (col, row);

    }

the reason i divide by 10 is because every tile is 10 units big.
i don´t have any pics to show the problem but the further away from origin i get in the y direction i get a offset by some factor. this only happens in the row and not in the col or x direction.
hope you guys can help me out or at least give me directions on how to fix this.

When you create your hexagon you multiply the X by 10 and the Z by 7.5 (10 * 0.75). In your ‘getHexAtCoord’ function you would need to divide the z by 7.5 instead of 10.

hey man thank you so much :slight_smile:
i kinda got it working now, but i can now see that when i move up in my x direction, my z direction begins to offset more and more. must be something in the GetHexCoord method ?
anyway here´s a snapshot of what´s happening. the pink box simply removes the hex tiles at it´s position.


and here´s the modified script:

public Tile getHexAtCoord(Vector3 Hexcoord)
    {
        int x = Mathf.FloorToInt (Hexcoord.x/10);
        int y = Mathf.FloorToInt (Hexcoord.z/7.5f);
        int col = x;
        int row = y + (x - (x % 2)) /2;

        print (y +"/"+row);

        return manager.GetTileAt (col, row);

    }