public void MoveTo(Vector2 newPosition) { Vector2 startPos = new Vector2(transform.position.x, transform.position.y); Queue <Node> nodePath = pathFindingManager.GetPathWithAStarAlgo(startPos, newPosition); StartFollowingPath(PathFindingManager.ConvertPathToWorldCoord(nodePath)); }
// Move to the given object // Check all the paths leading to tile neibourgh from this object // Choose the shorten path // if no path exist, return false public bool MoveToObject(GameObject targetObj) { if (targetObj == null) { Debug.Log("Error target object null"); return(false); } pathFindingManager.grid.ScanObstacles(); // important to check where the colliders are List <Node> potentialNodes = pathFindingManager.grid.GetFreeNeighbours(targetObj); string str = ""; foreach (Node node in potentialNodes) { str += " " + node.ToString(); } Debug.Log("Free nodes at:" + str); Queue <Node> pathToFollow = GetShortestPathToTargets(potentialNodes); if (pathToFollow != null && pathToFollow.Count > 0) { StartFollowingPath(PathFindingManager.ConvertPathToWorldCoord(pathToFollow)); return(true); } else { return(false); } }