Caculate path over grid

Hi all,

I am wondering if it is (relative easily) possible with Unity to calculate a path over grid tiles.
The concept is pretty simple; if you have chessboard, move from A->B without going diagonal.

I do have the position A and B of course, it are rounded numbers A(1,0,0) → B(4,3,0).
The only way i can think about this problem is to create a From Via To from every position to every position.
Of course (and i know) this is almost the worst possible solution.

Can someone give me a hint or tutorial? I looked into NavMeshPath but in my opinion it wasn’t very useful at all. I also watched a couple of tutorials but that didn’t help me either.

Thanks for reading and i hope someone can point me in the right direction

greetz.

The general purpose pathing most people use is called Astar (or A*).

For your solution you would create a graph of the grid, connected only along X and Y, not along diagonals.

Look for tutorials; there’s plenty of example code googling unity astar

If you want the motion to be just that simple, and only go in two straight lines, then you just need to get the difference in X, and Y, between A and B and move on each axis in turn.

Based on your grid position A is (1,-5), and position B is (4,-2).

So from A to B, the difference is (3, 3). So starting at A, you would move 3 up, then 3 right, to arrive at B.

Anything more complicated, like obstacles to avoid, or shortest possible path, A* is the way to go.

Thank you for you quick response. I will look into A*. Never heard of this before.
Thanks :slight_smile:

It is just an example. Maybe there will be some situations that it goes 3 up 2 left and than 1 up again, in that particular order. But it is a great starting point and indeed, it is simple.