//A*寻路 bool isFound = false; int traversalTimes = 1; List<NavTriangle> openList = new List<NavTriangle>(); //开放列表 List<NavTriangle> closeList = new List<NavTriangle>(); startNavTriangle.traversalTimes = traversalTimes; openList.Add(startNavTriangle); Debug.LogWarning("start:" + startNavTriangle.ID); int tmp = 10000; while (openList.Count > 0 && tmp > 0) { --tmp; NavTriangle curNode; curNode = openList[openList.Count - 1]; openList.Remove(curNode); closeList.Add(curNode);
if (curNode.ID == endNavTriangle.ID) { isFound = true; break; }
for (int i = 0; i < 3; ++i) { int index = curNode.neighborID[i]; NavTriangle neighboTriangle; if (index < 0) { continue; } else { neighboTriangle = navMeshData[index]; }
public Vector2 GetNextWayPoint(Vector2 way,List<NavTriangle> pathList,Vector2 end,refint curIndex) { var tri = pathList[curIndex];//当前路径点所在的三角形 var outLine = tri.GetLineByIndex(tri.outWallIndex);//获取当前三角形的离开边 int lastIndex1 = curIndex;//当前左侧那条边的所在三角形的index int lastIndex2 = curIndex;//当前右侧那条边的所在三角形的index Line l1 = new Line(way,outLine.start);//右侧那条边,路径点->离开边的起点 Line l2 = new Line(way,outLine.end);//左侧那条边,路径点->离开边的终点 Vector2 targetPoint1, targetPoint2;//下一个离开边的起点和终点(两条边下一个到达的位置)
var point = start; wayPoints.Add(point); int curIndex = 0; while (!(point == end))//如果获取到的拐点(路径点)不是终点 { point = GetNextWayPoint(point, pathList, end, ref curIndex);//获取下一个拐点 if (point == null) { returnfalse; } wayPoints.Add(point);//将获取到的点添加进路径点列表 }