x


Hex map movement problem

I'm working on a HoMM3 clone. I've got a hex map in form of a 2D array GameObject[,] hex = new GameObject[11,15] - 11 rows, 15 columns. Each hex has a script called Data attached to it which holds all it's variables such as:

int y - row number

int x - column numer

bool obstacle - true if there is a terrain obstacle on that field

bool unit - true if any other unit stands on that field

bool canMove - true if active unit can move on that field

Each unit has a variable int speed which is unit's movement speed in hexes. The problem is that units should not walk through other units and/or terrain obstacles, therefore they need to take longer paths. Pic related

alt text

Bear with the yellow highlight is the active monster, it has speed of 6 (as seen on his left side), but due to obstacles and units in north-east direction, he can't go everywhere where he normally could, as he must take the longer route.

I need this script to change canMove variable of each Hex to true if active unit can move there.

More info:

  • The map is made of 3D gameobjects seen from above with Orthographic camera.

  • Each unit is an empty GameObject with it's transform.position equal to transform.position of some Hex. It has a Plane child with the unit graphic.

  • I'm a C# man

more ▼

asked Oct 06 '11 at 05:54 PM

4illeen gravatar image

4illeen
63 33 37 38

Unless I am missing something you are already calculating all the hexes your unit can move to (The dark shaded ones). Can't you just set your flag when you shade the hex?

Oct 24 '11 at 08:10 PM Sammual12
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

cool enought, but I think you might be more/better help from a GameDeveloper forum such as: http://gamedev.stackexchange.com/

Because your problem seems to me, more of a HEX-tile question than an actual Unity problem.

more ▼

answered Oct 06 '11 at 07:37 PM

BerggreenDK gravatar image

BerggreenDK
2.4k 54 62 75

(comments are locked)
10|3000 characters needed characters left

Hex tile movement is actually really simple:

if you are using a 2-d array to represent your tiles, and you are at tile position X,Y:

  • For square movement, just allow movement to (X+1,Y), (X-1,Y), (X,Y-1), (X,Y+1)
  • For hex movement just allow square movement and add: (X+1,Y+1), (X+1,Y-1)

To visualize why this works, simply shift every other row in a square grid half a space to the right (sorry I don't have a picture off-hand)

more ▼

answered Oct 06 '11 at 09:34 PM

SilverTabby gravatar image

SilverTabby
1.9k 3 6 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1420
x19
x16

asked: Oct 06 '11 at 05:54 PM

Seen: 1322 times

Last Updated: Oct 24 '11 at 08:10 PM