Add Grid Offset to LevelGrid

Hi there, This course has been great! I learnt so much! I would like to request the ability to add an offset parameter, or use the transform of the LevelGrid GameObject’s transform, to offset the grid so that it can line up with Unity’s grid better, (this is more of an issue after the HexGrid Conversion). It seems like a simple thing, but I’m not sure of the best way to approach this. Thanks
image

The key is in the GetGridPosition() method…

    public Vector3 GetWorldPosition(GridPosition gridPosition)
    {
        return
            new Vector3(gridPosition.x, 0, 0) * cellSize +
            new Vector3(0, 0, gridPosition.z) * cellSize * HEX_VERTICAL_OFFSET_MULTIPLIER +
            (((gridPosition.z % 2) == 1) ? new Vector3(1, 0, 0) * cellSize * .5f : Vector3.zero);
    }

We simply need to add the offset to the end of this calculation. Fortunately, this offset will be the same regardless of whether z is Odd or Even because we’ve already baked that offset into the previous part of the equation…

    const Vector3 GlobalOffset = new Vector3(1,0,1);
    public Vector3 GetWorldPosition(GridPosition gridPosition)
    {
        return
            new Vector3(gridPosition.x, 0, 0) * cellSize +
            new Vector3(0, 0, gridPosition.z) * cellSize * HEX_VERTICAL_OFFSET_MULTIPLIER +
            (((gridPosition.z % 2) == 1) ? new Vector3(1, 0, 0) * cellSize * .5f : Vector3.zero) + 
            GlobalOffset;
    }

Thanks Brian!

That did the job!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms