Pathfinding in an array/list

Hi, i’m searching a way to do a pathfinding in an array/list of objects with some connections:
I have these:

[System.Serializable]
public class Room {
    public string id;
    public Vector3 position;
    public string[] nears; //stanze vicine
}

Where the nears array is other rooms id,
and i have already done this to verify the if the start and finish exist:

public List<Room> roomPositions;
private string roomStart = "Room 10";
private string roomFinish = "Room 23";

void RoomPathFinder (string roomStart, string roomFinish)

    {
        //find the starter//
        int starter = 0;
        for ( int i = 0; i < roomPositions.Count; i++ )
        {
            if ( roomPositions [i].id == roomStart )
            {
                starter = i;
                break;
            }
            if (i == roomPositions.Count - 1)
            {
                Debug.LogWarning ("The start doesn'w exist");
                return;
            }
        }

        //find the finish//
        for ( int i = 0; i < roomPositions.Count; i++ )
        {
            if ( roomPositions [i].id == roomFinish )
            {
                break;
            }
            if (i == roomPositions.Count - 1)
            {
                Debug.LogWarning ("The finish doesn'w exist");
                return;
            }
        }

        //to do some path finding

}

My question is: how can i find the path in an array of this tipe?
If the connection is backward in the array or if the connection is not direct, how can i find the path?
And how can i know if there are more ways, maybe with less connections?

Thanks for your help

You use a pathfinding algorithm. The most popular is astar. There’s a tutorial here:

It doesn’t matter if it’s a grid, or objects. You just measure the distance to the destination of each neighbor and eventually find the shortest path.

1 Like