Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Kiloblargh · Oct 26, 2012 at 08:11 AM · gridtilemappingsnaphex

How can I snap to hex tile centers in world x,z coords?

I was able to find the positions of the center vertices of the 6 hexes surrounding my first hex tile at (0,0) :

(13.5, 7.794228)

(0, 15.58837)

(-13.5, 7.794228)

(-13.5, -7.794228)

(0,-15.58837)

(13.5,-7.79)

I'm just not sure where to go from there.

Comment
Add comment · Show 2
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 ByteSheep · Oct 26, 2012 at 09:26 AM 0
Share

So are you trying to snap an objects position to the position of these hexes?
For example when dragging it around with the mouse? - Trying to get a clearer picture of what you are trying to do with the position values that you got from the hexes.
It sounds like you are just trying to snap the position of an object to the center position of the nearest hex.

So to do this you would want to:
• Get the x and z position of the object you would like to align
• Check the x and z values against the x and z values of each hex to find the nearest value
• Once you have found the hex which is nearest to the object, you can snap the object to that hexes x and z position

Let me know if this is what you are trying to do, or if I misunderstood your question :)

avatar image Kiloblargh · Oct 26, 2012 at 04:07 PM 0
Share

Yes. But step 2 is the problem, as the hex objects whose centers I'm snapping to don't yet exist. They're the things which I am trying to snap; to make a map editor and lay down the ground surface of my levels. All I can come up with now is a brute force approach where I manually create a hexagonal grid larger than my maximum map size by snapping the corners of the hexes to adjacent corners in the modeling program, then snap empty gameobjects to their centers, create a Transform[] array of all of those, then check through it for the closest point to the point that was clicked. But I know there's some simple elegant formula to find the coords of the nearest hexagon tile center to any point on the Cartesian plane given a certain size of tile, without knowing all the possible positions already, I just can't figure it out.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by .raptor · Oct 26, 2012 at 08:22 PM

I hope i have understood you correctly This is just a modified version of my square grid, seing as a hex grid can be achived with two square grids with 0.5 offset

 public static float gridSpacing = 1f; // Distance between hex centers
 public static Vector3 GetHexPosition (Vector3 convertPosition) 
     {

         float newx1 = Mathf.Round (convertPosition.x / gridSpacing) * gridSpacing;
         float newz1 = Mathf.Round (convertPosition.z / gridSpacing) * gridSpacing;
         
         float newx2 = Mathf.Round (convertPosition.x / gridSpacing) * gridSpacing + gridSpacing/2;
         float newz2 = Mathf.Round (convertPosition.z / gridSpacing) * gridSpacing + gridSpacing/2;
         
         Vector3 result1 = new Vector3(newx1, 1, newz1);
         Vector3 result2 = new Vector3(newx2, 1, newz2);

         if(Vector3.Distance(result1,convertPosition)<Vector3.Distance(result2,convertPosition))
         {
             return result1;    
         }
         return result2;

The code i use to call this function

 RaycastHit hit;
 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
 if (Physics.Raycast (ray, out hit))
 {
     gridPos = GetHexPosition(hit.point);
     this.gameObject.transform.position = gridPos;
 }

IT is attached to the semitransparent object i use to show what tile are marked, with a ray colliding with a flat terrain

Comment
Add comment · Show 1 · 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 Kiloblargh · Oct 26, 2012 at 09:14 PM 0
Share

I gave your answer a thumbs up for the help and I'm now headed in the right direction, but didn't accept it as the answer because 2 square grids offset by .5 do not make a hex grid, there's the square root of 3 involved on one of the axes which complicates things. (found this... http://playtechs.blogspot.com/2007/04/hex-grids.html)

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

11 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

Related Questions

Drag Rigidbody into slots? 2 Answers

Mapping Grid/Tile Systems 0 Answers

Hexagonal grid with 120 degree angles 1 Answer

Grid snapping 0 Answers

Chunks and 2d Hex-grid Generation 0 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