public IEnumerator GetPathAsync(Vector2 pFrom, Vector2 pTo, Astar astar) { //Debug.Log($"GetPath {pFrom}-{pTo}"); Path = new MovePath(); IsSearching = true; //if(!activeMap.IsWithinMap(pFrom) || !activeMap.IsWithinMap(pTo)) // goto end; if (!activeMap.IsWithinMap(pFrom)) { pFrom = activeMap.GetPositionWithinMap(pFrom); } if (!activeMap.IsWithinMap(pTo)) { pTo = activeMap.GetPositionWithinMap(pTo); } //while(!isInited) // await Task.Delay(100); if (!AstarAdapter.isInited) { goto end; } //handled better below //if(PathFinderController.OverlapsWithMapObject(pTo)) // return new MovePath(); SVector2 start = AstarAdapter.GetScaledVector(pFrom); SVector2 end = AstarAdapter.GetScaledVector(pTo); //if pTo is unreachable, try to find the closest node and navigate to it if (!astar.IsWalkable(end)) { //Debug.Log($"End {end} is unreachable"); SVector2?closest = astar.GetClosestWalkable(end, start); //Debug.Log($"closest = {closest }"); if (closest == null) { goto end; } else { end = (SVector2)closest; } } yield return(new WaitForEndOfFrame()); StartCoroutine(astar.FindPathAsync(start, end)); while (astar.IsSearching) { yield return(new WaitForEndOfFrame()); } var pathStack = astar.Path; List <Vector2> pathNodes = new List <Vector2>(); pathNodes.Add(pFrom); //skip the first node - otherwise the path starts with sharp turn. if (pathStack != null && pathStack.Count > 0) { pathStack.Pop(); } bool isPathValid = pathStack != null && pathStack.Count > 0; //try to join the next node with simple node Vector2 joinTarget = !isPathValid? AstarAdapter.GetScaledVector(end) : AstarAdapter.GetScaledVector(pathStack.Peek().Center); Vector2?joinPoint = GetJoinPoint(pFrom, joinTarget); if (joinPoint == null && !isPathValid) { goto end; } if (joinPoint != null) { pathNodes.Add((Vector2)joinPoint); } //add path nodes while (isPathValid && pathStack.Count > 0) { pathNodes.Add(AstarAdapter.GetScaledVector(pathStack.Pop().Center)); } pathNodes.Add(AstarAdapter.GetScaledVector(end)); Path = new MovePath(pathNodes, AstarAdapter.stepSize); end : IsSearching = false; //return path; }