Pathfinding

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 :frowning: would be great if anybody could help me out with some script. (if possible js but I’d also take c#)

A quick google search gives:

http://www.policyalmanac.org/games/aStarTutorial.htm

I already googled it and also found that page. The problem is that I understand how it is supposed to work but I am unable to code it

You are more likely to get a reply if you format your post correctly with the code brackets.

Sorry I just forgot to do it :wink:

I highly doubt anyone’s just going to code an A* algorithm for you. Try Unity’s built-in pathfinding, or look at the Asset Store. :slight_smile:

Ahem

OpenPath: Open source pathfinding for Unity

It’s rough around the edges still, but it’s being worked on, and it definitely works. Give it a try! At least you’ll find an A* algorithm in the code. Cheers!