- Home /
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.
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 :)
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.
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
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
Follow this Question
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