playMaker

Author Topic: Recognising adjacent Game objects in a grid  (Read 1320 times)

Fonkles

  • Playmaker Newbie
  • *
  • Posts: 10
    • View Profile
Recognising adjacent Game objects in a grid
« on: April 21, 2016, 07:57:11 AM »
Hello playmakers,

This is my first post asking for help so apologies if I miss anything important in my explanation.

I am making a digital version of a physical prototype I created.  The physical prototype has a hexagon grid and a player can spread his colour towards one ADJACENT hexagon every turn. This mechanic however is giving me a headache to create in unity with playmaker.

I have created a hexagon grid in unity, where every hexagon is a gameobject. So far I can change the colour of an hexagon with a 'mouse pick' and 'mouse pick event' and then change the material in the next state.

What I need is to restrict this to only work on hexagons that surround already coloured hexagons, i.e.; If player Red has one hexagon of his colour he/she should only be able to change the six hexagons surrounding it to red and not any other hexagon on the board.

The attached images Show more clearly what I mean.

I have been able to use playmaker so far for my projects but this one puzzles me a bit on how to do it. Some ideas I have gathered from browsing the forums and google are:

- Using an Array, I have never done this but am eager to learn it. So far some of the posts and video tutorial I have seen do not really adress my specific situation though.
- Using a Sphere collider on every hexagon that becomes red, or coloured, which will be big enough to collide with the hexagon game objects around it. This will mean a lot of these will be created and I am unsure if this will destroy performance.
- Ditch changing the materials and use a red gameobject to move to a specified hexagon if I click it.

I was hoping some of you could give me advice on how you think its best to proceed or a sight nudge in the right direction. Any help is appreciated.

Thanks in advance.

Martijn
« Last Edit: April 21, 2016, 08:01:10 AM by Fonkles »

Chizzler

  • Junior Playmaker
  • **
  • Posts: 67
    • View Profile
Re: Recognising adjacent Game objects in a grid
« Reply #1 on: April 21, 2016, 02:30:38 PM »
You definitely want to go down the Array route for performance reasons.

If working on a square grid it's simple to find the adjacent neighbours. You grab the Index of your starting tile either through a "Mouse Pick" action, followed by "Array Index Of" OR better yet, an algorithm to determine the selected tile based on the position of your mouse. Because your tiles are uniform in size and position, you can round your X,Y,Z Positions to the nearest tile.  So for example, my tiles are 5x5, I mouse pick at position X=11,Z=8   From this I can "round to nearest" in increments of 5. Giving us the tile 2 across (10) and 2 down (10)

A1(0),A2(1),A3(2)
B1(3),B2(4),B3(5)
C1(6),C2(7),C3(8)

So, we've pick B2 as our starting position. This is the 5th entry in our Array (It's Index is 4 - Arrays start at 0)   to get the left or right tiles, we can add or subtract 1 from that index. To get the top and bottom neighbours, we'd add or subtract the row Size (3) from the starting index.

Now, a Hexagonal grid works slightly different because there's an offset on the rows. You'll need to work out if you're on an odd numbered row or an even one. How to go about this eludes me at the moment, but once you've worked out which row you're on, you can adjust which Index values are neighbours by adding or subtracting an additional 1.

Sorry I can't give you more details, but I hope this sets you on the right track, or someone more familiar with Hexagonal grids can help with figuring out the algoritm to determine which row you're on.

Fonkles

  • Playmaker Newbie
  • *
  • Posts: 10
    • View Profile
Re: Recognising adjacent Game objects in a grid
« Reply #2 on: April 22, 2016, 04:09:36 PM »
I will research the array route and see if i can get that to work. Or I work around it by changing the playing rules a bit if I dont make it in time.

Thanks for the advice Chizzler!