Support Forum

Moving characters in a turn-based game using a point graph

Hi everyone!

The title may not be perfect, but I didn’t have a better idea, sorry.
I’m working on a turn-based game, which will use hexagon-based movement, just like Heroes of Might and Magic.
I’m using a point graph, the nodes are set, the pathfinding works fine and I can limit the amount of steps of the characters, thanks to Aron who helped me in an earlier topic.
Obviously, I don’t want characters and enemies to step on each other, so I’m storing the states of the hexagons in a matrix, for example:
GridState[x,y] = 0 means that the position is empty and reachable,
GridState[x,y] = 1 means that a player character is on the position,
GridState[x,y] = 2 means that an enemy character is on the position,
GridState[x,y] = 3 means that the position is too far from the current player.

When the position is occupied (for example with the “enemy1” gameobject), I’m making the node unwalkable with this code:

AstarPath.active.AddWorkItem (() => {
var currNode = AstarPath.active.GetNearest (enemy1.GetComponent<Transform> ().position).node;
currNode.Walkable = false;
});

So far so good.
The first problem is, when I’d like to attack an enemy. If the enemy is in a reachable range, the player character will start to walk towards it, and because the enemy’s node is unwalkable, the player will stop on the closest node, and starts the attacking animation. It works, however the nearest node is always one line above, or one line below the enemy, and never at the same line. Maybe because the width of my hexagons are greater than their height? Check screenshot:

screenshot1

It looks awkward for most of the animations, so I’d like to stop the players in front of the enemy. How can I tell the player, that he should stop at position1 instead of position2?

screenshot2

Also, it seems that the enemies are not working properly, because they can walk over each other, and they can step on the players’ positions too. Their pathfinding code is the same as for the players.
In my GameControl script I’m updating the state of the grid at the beginning of the Update function: first step is to clear everything (setting all values to 0), second step is to set occupied positions (with values 1 and 2), third step is to set unreachable positions (with values 3). Is there a chance that the enemy’s pathfinding runs after the clearing happens, and before setting the occupied positions?
I mean:
step1 (GameControl): all values are set to 0
step2 (EnemyScript): enemy’s pathfinding runs, he thinks that all positions are free
step3 (GameControl): occupied positions are set to 1 and 2

Is it possible, or something else causes the trouble?
Thanks in advance for any help!

Hi

You may want to use the built-in hexagonal graph instead. It has a special case for exactly this use case.
Create a grid graph and set the ‘Shape’ setting to Hexagonal.

Alternatively you could use the MultiTargetPath (see https://arongranberg.com/astar/docs/multitargetpath.html)

Thanks for the answer.

The reason for me to use a point graph was that I couldn’t find a correct setting for a hexagonal grid graph to handle what I wanted.
I’ve checked it again, but I can’t add different values for width and height and also, the grid is rhombus shaped, and I wanted something different.
Am I doing something wrong?

screenshot3