- Home /
PathFinding with fixed waypoints problem
I got this idea to develop but I cant get my head around this (beginner really). What you can see on the image is a map with locations (big circles) and bunch of connections with "stops". What I would like is when i drag my player from one location to another, to calculate a shortest 'route' and then, every turn (game is turn based) advance one stop toward final destination. What i have for now is for each of the locations (the big circle one), i have a class :
[System.Serializable]
public class Connection
{
public Location NeighborLoc;
public int Distance;
}
public Connection[] connections;
Where for each of the locations i have a list of "neighbor" locations with fixed distance to them (int) - basically a number of small white circles ("stops"). Then i have a function :
public void CalculateRoute(Location from, Location where)
But, whatever i do in this f-ion I end up in a infinite loop, crash unity or, just never calculate a route :). I tried basically going trough all connections for a location, then for each of those connections, find an endpoint and recursively call CalculateRoute on that location and 'where' locations, this usually ends up as an infinite loop, and, even then, I'm not sure how to go from there (how to find a shortest route, and 'store' this route for later advancing every turn, how to avoid turning back to start location). I'm guessing I should take a totally different approach here (some kind of pathfinding or something?? )
Thanks for any help :)
Answer by toddisarockstar · Nov 17, 2019 at 06:42 AM
for this situation i wouldnt use a grid style of pathfinding. you could acheive your movement starting with a list or array of all your points.
the main array would include nested lists or arrays of all the connections to the other indexes in the main array.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class points : MonoBehaviour {
//drag and drop location markers here for reference
public GameObject [] spots = new GameObject[50];
// inside this array put all the connections to the index numbers of other points
public int [][] connect=new int[50][];
void Start () {
connect [0] = new int[]{2,3};
connect [1] = new int[]{3,4,5};
connect [2] = new int[]{6,7};
// continue this process for all points
}
}
the two dimentional array would give you data you need to acheive the movement for pathfinding.
you would do a flood-fill style algo till you find your end point marking the counted steps on each point along the way.
this way after you find your finish you could trace backwords looking for consecutive numbers down to output your path.
this process is similar to A-star but actually a bit simpler.
if you are still looking to code this yourself let me know and i could help at some point this weekend.
I like this idea, what i did actually was allot more 'dumb' but it works :) : I kept my "connection" class but did it like this:
[System.Serializable]
public class Connection
{
public Location goal;
public Location direction;
public int Distance;
}
public Connection[] conns;
Then i'll manually, in editor, fill these for each of the locations (ill add all other locations, and a direction for each of them (basically a first step towards the goal, i calculate this manually, just by looking at the map and counting the steps ). Then it's just looking trough all connections in conns, finding the one with the goal, and appropriate direction, go to direction 'node' - repeat the process.