So I’m making an endless RPG type board game, but I’m having trouble with setting the route for player to move.
On “Start” function I instantiate custom amount of “Road” prefabs (“Road” is a 10x10 green shape which has custom “road cut out” filled with 1x1 brown road tiles), which have different directions. When these prefabs are instantiated I parent them to the GameObject that has Route script attached to it and put only brown tiles into a list that makes up the road. Then using OnDrawGizmos I pick every single tile and try to plot the road with green line, but in the picture it’s clear that green line is all messed up and doesn’t follow a path (By the way the start is bottom right). In list I can see that tiles are put in random order, but I can’t seem to figure out how to sort or put them in a list so that they create a normal path.
public class Route : MonoBehaviour
{
GameObject[] childObjects;
public List<Transform> nodeList = new List<Transform>();
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
FillNodes();
for (int i = 0; i < nodeList.Count; i++)
{
Vector3 currentPos = nodeList*.position;*
if(i > 0)
{
Vector3 prevPos = nodeList[i - 1].position;
Gizmos.DrawLine(prevPos, currentPos);
}
}
}
void FillNodes()
{
nodeList.Clear();
childObjects = GameObject.FindGameObjectsWithTag(“Tile”);
foreach (GameObject child in childObjects)
{
if(child != transform)
{
nodeList.Add(child.transform);
}
}
}
}