Hey,
I need some help on a pathfinding script I’m trying to write. So it is supposed to work like some kind of A* pathfinding.
Therefore I created street-objects which are each made of two waypoints. So I am beginning with
var streets : Street[] = new Street[myNumberOfStreets];
var currentStreet : int;
var target : Transform;
class Street
{
streetNumber : int;
waypoint1 : Transform;
waypoint2 : Transform;
connectedStreets : Street[];
distance : int; //which is calculated by Vector3.Distance(streets[thisStreet].waypoints2.position, streets[thisStreet].waypoints1.position)
}
function Awake()
{
for(var i : int < 0; i < streets.lengtH, i++)
{
streets[a] = new Street;
}
}
// after that all streets get assigned in a different script
function FindClosestWaypoint()
{
var distance : Array = new Array();
for(var i ......, i++)
{
distance.Add(Vector3.Distance(transform.position, streets[i].waypoints1));
}
distance.Sort();
for(var b ......, b++)
{
if(Vector3.Distance(transform.position, streets[i].waypoints1) == distance[0])
{
currentStreet = streets[b].streetNumber;
}
}
}
function FindClosestWaypointToEnd()
{
basically the same as the one above
}
So this gives me the street where I want to start and it gives me the street where I want to end. I tried it out and it works fine.
now I need to find the path and thats where I am getting the problem.
I now want to start to and check which is going to be the shortest way. So I need to check for each connected street (lets name them a and b) their connected street (which might be c, e and f). And then I need to check for the connected streets of c,e and f. And I need check as long as either I return to a street I already check, which would mean that I would have made circle or until the the connected street equals the end street.
And I have no clue how to write that
would be great if anybody could help me out with some script. (if possible js but I’d also take c#)