- Home /
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
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
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?
Answer by BerggreenDK · Oct 06, 2011 at 07:37 PM
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.
Answer by SilverTabby · Oct 06, 2011 at 09:34 PM
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)
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Save a precalculated grid path into a jagged 2d array ready to be loaded when need 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Adding elements to a 2d array. 2 Answers
How do I make my character stop falling when you press a button? 0 Answers