How can I find shortest 2d path but without moving diagnolly?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By viniciused26

I have a Level scene with a sprite of the map. I’m adding instances of an enemy scene in a node in this Level scene. I want to make an enemy move to a point (player or other enemies) without colliding with other stuff and I only want to make them to move horizontally or vertically.

Enemies spawn in random positions in the Level scene, but only in a specific rectangle area. I want them to move 16 pixels to the direction it needs, to another empty space until they reach their destination.

Thanks.

Level scene and Nodes: Imgur: The magic of the Internet

:bust_in_silhouette: Reply From: JimArtificer

Godot has a built-in implementation of the A* algorithm, which is commonly used to calculate the shortest distance between two locations.

If you only generate connections between adjacent points it will not suggest diagonal moves. This is a pretty good tutorial video:

https://docs.godotengine.org/en/stable/classes/class_astar.html