private Vector2 DecideNextDirection() { // Get the possible directions out of the current tile Tile currentTile = tileMap[(int)truncatedPosition.x, (int)truncatedPosition.y]; IList <Vector2> directions = currentTile.GetDirections(); // Calculate which of the possible directions is the closest to the target Vector2 closestToTarget = new Vector2(1000, 1000); foreach (Vector2 dir in directions) { // The opposite direction of the current one is discarded if (!VectorUtils.AreOpposite(ghostController.GetDirection(), dir)) { if (Vector2.Distance(ghost.position + dir, target) < Vector2.Distance(ghost.position + closestToTarget, target)) { closestToTarget = dir; } } } lastDirectionDecided = closestToTarget; return(closestToTarget); }