How to align hexagons for randomize map generation

Hello! I was wondering how to tesselate hexagons during Perlin Noise generation. Whenever I start the game, it makes the random map but it doesnt align perfectly as I wanted it too. They overlap eachother. I will much appreciate your help!

local function generateMap()
	local hexHeight = math.sqrt(3) * tileSize / 2
	local hexWidth = tileSize

	for x = -oceanBuffer, mapSize + oceanBuffer - 1 do
		for z = -oceanBuffer, mapSize + oceanBuffer - 1 do
			local newTile

			-- hexagonal grid position
			local xOffset = (x % 2) * (hexWidth * 0.5)
			local xPos = x * (hexWidth * 0.75)
			local zPos = z * hexHeight + xOffset

			if x >= 0 and x < mapSize and z >= 0 and z < mapSize then
				local noiseValue = perlinNoise(x, z) + cf.GetVal((x - halfSize) / halfSize, (z - halfSize) / halfSize)
(Cloning the stuff here)

			newTile.CFrame = CFrame.new(xPos, 0, zPos) * CFrame.Angles(0, math.rad(30), 0)
			newTile.Parent = Workspace
		end
	end

1 Like

I would use this way:

  • Generate map grid that are squares X / Y studs
  • Place hexagons onto the grid in specific order or using algorithm
  • Rotate or move hexagons soo they will match