Grid movement system

So, i have this arena that is made up of grids:
image

So the goal is for the grey right to reach the red rig, but i don’t want it to just go any way it wants

so his movement options would be go forward, back, or sideways one block
like this:
image

So basically what it would do is check if there are any enemies in his range (i already scripted detecting that), and if there arent move let’s say forward one block, but it should move the direction that is the shortest way to the target

so in this case this would be the path:
image

so question is how would i go about scripting this? specifically finding the best path

I would probably use an implementation of Dijkstra’s Algorithm, treating the grid squares like nodes. There may be a simpler algorithm, however with Dijkstra you can also create complex grids which will account for walls (or technically, excluded/ missing nodes). You could also use a form of A* pathfinding.

Dijkstra’s algorithm
A* search

3 Likes

ive taken a look at both, and it does look like what i would need, but i have no idea how i would implement this in a roblox script, and i haven’t found anyone do it either

If you wanted a very primitive taxicab like navigation you could iterate the difference between the grey and red rigs’ x,y coordinates in that order. Like:
Grey at (2,3), Red at (3,1)
iterate Grey’s ‘x’ position from 2 to 3 to match Red’s
iterate Grey’s ‘y’ position from 3 to 1

Plenty room for improvement if you wanted to implement diagonals or determining which axis to move on first based on the difference in magnitude on the axes.