Support Forum

Help Matching the Hexagon Grid Graph to the 2018.2 2D Hexagon Grid

Does anyone have any idea on how to match the Unity hexagon grid from the new 2D tools? I’ve been trying for 2 hours to get them to work properly but the node sizes are not the same and I can’t quite get them to match.

For Clarification my game is not a grid based 2D game but uses tilemap to make the levels.

Here’s my Unity Grid settings:
Hexagon point top
Cell Size: (1, 1.154701, 0) (By Default they are squished to fit 1x1 unit, this makes them approximately regular hexagons)
Layout Hexagon
Cell Swizzle: XYZ

Here’s my Astar Path Settings
Shape: Hexagon
2D: True
Width:100
Height:100
Node Size: 1.23(Best I could get to match the unit width)
Center:
(0.25, 0.425, 0)
Rotation: 15
Use 2D physics: True
Collision Testing: True
Collider Type: Circle
Diameter: 0.75
Obstacle Layer: Walls(my Hex grid has a tilemap collider on it with a layer wall)

Unfortunately the grid is not precise enough this way and after going only a few tiles from the center, it no longer aligns close enough to fit into the spaces near the walls things can’t pass through single spaced openings etc.

Dropping it down to Diameter 0.7 allows it to work though those spaces but it overlaps some of my Tiles and on occasion it misses a space that should be open

Near the center it works great:

And Finally 70 or so tiles right of center:

Anyone know a way to get the tilemap to match better? Also I guess this could also be a feature request to have default settings to match the Unity 2D tilemaps from the 2018.2 release.

Hi

Yeah, I definitely should add a feature to match the size to a tilemap. In the meantime, here is a picture that shows all the different sizes of the hexagons.

For example if their width (i.e. distance between two edges on the hexagon) is 1 world unit, then the node size should be sqrt(3/2) ≈ 1.224744871 world units. This coincidentally looks very close to the value that you had found.
Or if the hexagons are squished so that their diameter is exactly 1 world unit, then the node size should be 3/(sqrt(2)*2) ≈ 1.060660172 world units.

Version 4.2.4 is now released which has additional inspector tweaks to make it possible to set the diameter/width of the hexagons directly instead of having to use complicated conversion factors.

1 Like

Is it possible to also set the height of the hexagon, different from the width?
I am still having trouble lining up the hexagon grid. Maybe it’s irregular sized hexagon?

Hi

If you cannot use either the width or the diameter of the hexagon to set its dimensions, then I think your hexagon might not actually be a hexagon. Try to check again if it actually is one.

Here are the settings I tried, just doesn’t seem to fit very well…

Cell Swizzle: XYZ
Cell Size: X:2.56 Y:2.56

Cell Size: X: 1 Y: 1

Hi

Your hexagons have far from equally long sides, so they aren’t really hexagons. Maybe try adjusting your graphics?

Update on this, I was able to match the settings, I was using pointed top in my example but switchted to flat top since then. Here’s my settings:

Unity Grid:
Cell Size: (0.8660254, 1, 1)
Cell Gap: (0, 0, 0)
Cell Layout: Hexagon
Cell Swizzle: YXZ

Astar Path settings:
Shape: Hexagonal
2D: Checked
Hexagon dimension: Diameter
Hexagon Diameter: 1
Center: (0.38, 0.22, 0)
Rotation: 345

image
For point top it’s very similar

Unity Grid:
Cell Size: (0.8660254, 1, 1)
Cell Gap: (0, 0, 0)
Cell Layout: Hexagon
Cell Swizzle: XYZ

Astar Path settings:
Shape: Hexagonal
2D: Checked
Hexagon dimension: Diameter
Hexagon Diameter: 1
Center: (0.22, 0.38, 0)
Rotation: 15

image

Finally to get your regular polygon sprites to match the size make sure the PPU settings are the “point to point” length, not the “edge to edge” width example my flat top hex image is 294x256 so the PPU is 294

1 Like

@Snowdrama Nice! I think it might be easier for you to match the dimensions if you set the Hexagon dimension to Hexagon Width instead of Diameter though. I think the scale factors should become nice round numbers then.

I’ll try that not sure what values to use then.

I’m not a math person so for others who stumble on this, I’ll explain how I reached the conclusion for the values The default unity grid is 1:1 ratio hexagons.

To fix this I need to either scale up the vertical axis, or scale down the horizontal. I followed This site for the ratio which has the ratio listed as sqrt(3):2 so I got a unity grid size of (sqrt(3), 2, 1) or (1.732051, 2, 1) which I then reduced to (sqrt(3)/2, 1, 1) or (0.8660254, 1, 1)

Which makes them regular.

Hopefully this makes sense.

Aaah, I see. Your hexagons were originally not hexagons at all. Yeah, if they are not actually hexagons then it would have been hard to match them to the hexagon graph.

1 Like

Yeah hahaha. I have no idea why Unity did that with the Hexagon tile grid but that was the trick. Getting the unity grid settings that allowed the hexagons to be regular hexagons was what was needed(besides getting the offset to match)

What to do if you can’t scale the Unity grid because you’re using tiles that are sized to that? Possible to size the A* grid non-uniform?

@Tom

If you switch the graph to the ‘Advanced’ mode you can adjust the aspect ratio to stretch the nodes out, or the isometric angle to stretch them in the other direction.

1 Like