public IEnumerator MoveUnitTo(int TargetX, int TargetY) //this one will be the one thats called in general and will allow to stop { if (IsUnitMoving) { StopMoving = true; while (IsUnitMoving) //this can cause an infinite loops but whatever { // Debug.Log("Waiting"); yield return(null); } } List <MapTile> Path = new List <MapTile>(); if (MapLocal.FindTile(TargetX, TargetY).Walkable) { Path = MapLocal.Pathfinding(GridPos, new Vector2Int(TargetX, TargetY)); if (Path != null) { IsUnitMoving = true; if (IsThisMainP) { UI_MLocal.ShowPath(Path); //Show the UI } //Start the walk thing for (int x = 0; x < Path.Count; x++) { //In here I start the movement for the next tile //Yielding until its completed yield return(StartCoroutine(MoveNextTile(Path[x].X, Path[x].Y))); if (StopMoving) { StopMoving = false; break; } if (IsThisMainP) { UI_MLocal.ClearSpecPath(x); } } IsUnitMoving = false; } else { Debug.Log("Path is null"); //This is a way to fix it for the AI if (this.GetComponent <AI_Handler>()) //this is kinda cheap { this.GetComponent <AI_Handler>().UpdateMovementNow = true; } } } /* * List<MapTile> MyPath = new List<MapTile>(); * Vector2Int DebugPos = new Vector2Int(TargetX, TargetY); * if (MapLocal.FindTile(TargetX,TargetY).Walkable == true) * { * // if (!IsUnitMoving) * // { * MyPath = MapLocal.Pathfinding(GridPos, new Vector2Int(DebugPos.x, DebugPos.y)); * // StartCoroutine(PathMoveAnim(MyPath)); * if (MyPath != null) * { * StartCoroutine(MoveToTileAnim(MyPath)); * } * else * { * Debug.Log("Path couldnt be found"); * } * // } * * } else * { * Debug.Log("Target Tile isnt walkable"); * } * * * * * //This should be in another function to be more organized tho * * // PlaceTarget(DebugPos.x, DebugPos.y); Add this somewhere else */ }