//delete this M6 /* * void Update(){ * //if(Input.GetKeyDown(KeyCode.K)){ * FindPath (seeker.position, target.position); * //} * } */ //modify this M6 //void FindPath(Vector2 from, Vector2 to){ Vector2[] FindPath(Vector2 from, Vector2 to) { Stopwatch sw = new Stopwatch(); sw.Start(); //add this M6 Vector2[] waypoints = new Vector2[0]; bool pathSuccess = false; Node startNode = grid.NodeFromWorldPoint(from); Node targetNode = grid.NodeFromWorldPoint(to); //add this M6 startNode.parent = startNode; //add this M6 after class Grid2D if (!startNode.walkable) { startNode = grid.ClosestWalkableNode(startNode); } if (!targetNode.walkable) { targetNode = grid.ClosestWalkableNode(targetNode); } if (startNode.walkable && targetNode.walkable) //----- //List<Node> openSet = new List<Node>(); { Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); /* * for (int i = 1; i < openSet.Count; i++) { * if (openSet [i].fCost < currentNode.fCost || * openSet [i].fCost == currentNode.fCost && * openSet [i].hCost < currentNode.hCost) { * currentNode = openSet [i]; * } * } * * openSet.Remove (currentNode); */ closedSet.Add(currentNode); if (currentNode == targetNode) { sw.Stop(); print("Path found: " + sw.ElapsedMilliseconds + " ms"); //modify this M6 //RetracePath(startNode, targetNode); //return; //to this pathSuccess = true; break; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } //add this if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); } return(waypoints); } //----