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\$
2
  • 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\$ Commented Sep 27, 2014 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
    Commented Sep 28, 2014 at 17:13

2 Answers 2

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 :)

\$\endgroup\$
7
  • \$\begingroup\$ Thanks! I have never used SphereCast in my programs. I will look into it but I assume it works like Raycast \$\endgroup\$
    – ckzilla
    Commented Sep 28, 2014 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
    Commented Sep 28, 2014 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
    Commented Sep 28, 2014 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
    Commented Sep 28, 2014 at 19:09
  • \$\begingroup\$ Can you please give a use case scenario for such neighbor search? \$\endgroup\$
    – lvictorino
    Commented Sep 29, 2014 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

\$\endgroup\$
2
  • \$\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
    Commented Sep 28, 2014 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
    Commented Sep 29, 2014 at 7:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .