Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Waffle Dragon · Mar 22, 2014 at 11:26 AM · snapsnappinghexagonhex

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.

alt text

untitled.png (63.4 kB)
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Destran · Mar 22, 2014 at 11:53 AM 0
Share

select all your hex objects

go to edit-->snap settings -- > snap y

avatar image Waffle Dragon · Mar 22, 2014 at 12:09 PM 0
Share

that doesn't solve what im trying to do

avatar image Destran · Mar 22, 2014 at 12:16 PM 0
Share

sorry misunderstood your question. Did you mean how to make something like the tiles like in the Civilization games?

alt text

civ.jpg (397.0 kB)
avatar image Waffle Dragon · Mar 22, 2014 at 12:26 PM 0
Share

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

avatar image Destran · Mar 22, 2014 at 12:46 PM 0
Share

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:

https://www.assetstore.unity3d.com/#/content/2392

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Mar 22, 2014 at 12:45 PM

It's actually quite simple. Take a look at this picture:

Hexagon

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waffle Dragon · Mar 22, 2014 at 01:46 PM 0
Share

I'm unsure how to implement this and I'm trying to snap on X and Z not Y

avatar image Bunny83 · Mar 22, 2014 at 02:02 PM 0
Share

Ohh, i thought you already know the snap settings in Unity ;) Scroll down to "Unit Snapping"

avatar image Waffle Dragon · Mar 22, 2014 at 02:21 PM 0
Share

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

avatar image Bunny83 · Mar 22, 2014 at 02:51 PM 0
Share

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.

avatar image Waffle Dragon · Mar 22, 2014 at 02:58 PM 0
Share

awesome its working now, I just had the radius wrong, thanks.

Show more comments
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Create Grid and Snap Rigidbodies to Grid 1 Answer

An UI element will follow the mouse and snap to a grid, but why is the grid shifted after every game start? 0 Answers

Can't generate 3D meshes via script 1 Answer

Add object OnMouseDown 1 Answer

Toggle snapping in editor 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges