0
\$\begingroup\$

I have a hex tiled model like attached below. Each of the hex tiles are separate objects meaning I can access each of the hex tiles individually inside Unity

enter image description here

Now in my program I want to be able to get the 6 neighboring tiles of the selected tile. At first I thought I would get the 6 edges of the current tile and then check to see if any of the remaining tiles have those edges in common but that would be very expensive if I want to compute neighbors of all the tiles.

Any ideas??

\$\endgroup\$
  • 1
    \$\begingroup\$ One of the best lessons I learned a job or two ago was to keep a little separation between the data model (hex grid math) and visual representation, and to ease going back and forth with a mapping layer. While it seems like a big waste of resources, being organized like that solves many problems like the one you're experiencing. \$\endgroup\$ – Patrick Hughes Sep 27 '14 at 22:26
  • \$\begingroup\$ Yes I agree. But basically what I am trying to do is colorize my hex tiles with random colors but I want some of my neighbors to have the same color creating a region. The model was built in Houdini and imported as FBX with Maya. So I am stuck working with what I have at the moment. But I am trying to find the simplest solution possible even if its not highly efficient \$\endgroup\$ – ckzilla Sep 28 '14 at 17:13
1
\$\begingroup\$

As I can see the hex game objects have a mesh collider. So you can use SphereCastAll using your hex center position as origin and hex radius * 2 as radius to grab every neighbors colliders.

It's surely not the most elegant way to do it but it sure avoid headaches :)

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thanks! I have never used SphereCast in my programs. I will look into it but I assume it works like Raycast \$\endgroup\$ – ckzilla Sep 28 '14 at 16:53
  • \$\begingroup\$ Though I am not sure how I could use spherecast if I would want to get neighbors of all my tiles. It would be like constantly changing the position of the source of the spherecast around the sphere \$\endgroup\$ – ckzilla Sep 28 '14 at 17:11
  • \$\begingroup\$ SphereCast is a good option if you want, for example, to find the neighbor on user input. If players click on a specific hex, then you SphereCastAll to act / display the neighbors. I thought you wanted to perform that kind of action. \$\endgroup\$ – lvictorino Sep 28 '14 at 19:08
  • \$\begingroup\$ Actually, if you save neighbors in a list it won't cost a lot as you could easily retrieve already visited neighbors. \$\endgroup\$ – lvictorino Sep 28 '14 at 19:09
  • \$\begingroup\$ Can you please give a use case scenario for such neighbor search? \$\endgroup\$ – lvictorino Sep 29 '14 at 15:21
0
\$\begingroup\$

you could keep references to them in a kind of an array like this so you'd get the neighbours just with indexing, like in a 2d grid: https://stackoverflow.com/questions/6661169/finding-adjacent-neighbors-on-a-hexagonal-grid and https://stackoverflow.com/questions/4585135/hexagonal-tiles-and-finding-their-adjacent-neighbourghs/15524441#15524441

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I thought of storing and indexing them in an array. Well I already have stored them in a Map for another part of the game. But I don't understand how by storing them in an array would I be able to reference their neighbors because they would all get randomly stored in the array. \$\endgroup\$ – ckzilla Sep 28 '14 at 17:17
  • 1
    \$\begingroup\$ Don't store them randomly but ordered like in those links. So then you can do [current_index + 1] and such to get the neighbours directly. \$\endgroup\$ – antont Sep 29 '14 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.