- Home /
Hex Object Snapping
I'm trying to snap hexagon objects side by side with a very tiny gap so i can quickly build levels but I'm not sure how to do this.
select all your hex objects
go to edit-->snap settings -- > snap y
sorry misunderstood your question. Did you mean how to make something like the tiles like in the Civilization games?
yea but ins$$anonymous$$d of one large terrain, it would be made up of hex objects like in the pic above, and with out the hex lines being displayed like they are in civ
I'm not experienced enough with scripting to tell you how to do it (sorry). But maybe these tutorials can help?
http://gamedev.stackexchange.com/questions/15881/hexagonal-grid-tiles-tutorials
https://tbswithunity3d.wordpress.com/2012/02/21/hexagonal-grid-generating-the-grid/#more-23
And if you have money there's an asset for hex grid tile:
Answer by Bunny83 · Mar 22, 2014 at 12:45 PM
It's actually quite simple. Take a look at this picture:
As you can first you need to know the radius of your hex tile. That is half of the list between two opposed corners. The two axis need different snap settings. In my example you need 3/2 the radius for clamping on the x axis and sin(60) times the radius on the y axis.
If you want a little gap, just add s bit to the radius. However i would suggest to "not" have a gap since that could confuse you even more when it comes to calculating the tile position in code.
edit
I forgot to mention that your tiles pivot should be at the center of the tile ;) Also that does only work for regular hexagons
Also keep in mind that not every snap position is a position in the hex grid, but all hex-grid-positions are in the snapgrid.
second edit
This solution is ment for use in the Unity editor. If you want to do that in game it's better to calculate the real hex tile positions manually. However I understand the question that you just want a guide / helper to positioning your hex tiles in the editor, right?
third edit
Here's another example in concrete numbers:
Just assume your tile radius is 10 so the diameter is 20
To get the grid interval for X you just multiply 10 by 3/2 which is 15
To get the grid interval for Z you multiply 10 with sin(60°)
sin(60°) is a constant which is 0.86602540378443864676372317075294 (that's how long the windows calculator displays the number)
So for Z you would use an interval of 8.6602540378443864676372317075294
If you add a little fraction to the radius to get a gap it would look like this:
new radius = 10 + 0.1 = 10.1
X Snap = 10.1 * 3/2 = 15.15
Z Snap = 10.1 * sin(60) = 8.7468565782228303323136040246047
I'm unsure how to implement this and I'm trying to snap on X and Z not Y
Ohh, i thought you already know the snap settings in Unity ;) Scroll down to "Unit Snapping"
is there a better way to find the radius because i just counted the squares on the grid witch is 4.5 and i add 2.25 for x and 3.897 for z in the snap settings but this did not work
Well, you could add a box collider in the editor. The box collider usually resizes itself to enclose the whole mesh. Just use the larger value.
Also keep in $$anonymous$$d to hold the "Control key (Command on $$anonymous$$ac)" while dragging an object.
awesome its working now, I just had the radius wrong, thanks.
Answer by Berenger · Mar 22, 2014 at 12:43 PM
Let's have fun with some math :
First, you need the length of the segments going from the center of a shape to the middle of the edges. Each edge form a triangle with the center as third points and with all the angles at 60°. That means 2 rectangular triangles per edge -> 1 extremity, center of the edge, center of the hex. Conclusion, the segment's length is Cos(pi / 6) / radius.
Now, to figure out the relative position of the hex to snap, I suggest Quaternaion.AngleAxis(x, Vector3.up) Vector3.forward segmentLength, x being 30, 90, 150, 210, 270 or 330.
Your answer
Follow this Question
Related Questions
Scroll Rect Snapping 0 Answers
Snap All Axes Hotkey? 0 Answers
cannot get snap to grid to activate 0 Answers
Snapping UI sprite? 0 Answers
Why can't I get snapping to work? (Mac) 4 Answers