public bool IsAnotherSnakeTailNearMe(float maxDistance, out Vector3 directionToReachEnemySnakeTail, out GroundPiece enemyTailGroundPieceToReach) { List <GroundPiece> possibleEnemyTailGroundPieces = new List <GroundPiece> (); List <Vector3> possibleDirections = new List <Vector3> (); directionToReachEnemySnakeTail = Vector3.zero; enemyTailGroundPieceToReach = null; if (lastReachedGroundPiece.HasGroundPieceOnEast()) { for (int i = lastReachedGroundPiece.indexInRow + 1; i < lastReachedGroundPiece.row.groundPieces.Count; i++) { GroundPiece pieceToCheck = lastReachedGroundPiece.row.groundPieces [i]; if (Vector3.Distance(pieceToCheck.transform.position, transform.position) > maxDistance) { break; } if (pieceToCheck.collectingSnake != null) { if (pieceToCheck.collectingSnake == snake) { break; } if (pieceToCheck.collectingSnake != snake) { possibleEnemyTailGroundPieces.Add(pieceToCheck); possibleDirections.Add(transform.right); break; } } } } if (lastReachedGroundPiece.HasGroundPieceOnOvest()) { for (int i = lastReachedGroundPiece.indexInRow - 1; i > 0; i--) { GroundPiece pieceToCheck = lastReachedGroundPiece.row.groundPieces [i]; if (Vector3.Distance(pieceToCheck.transform.position, transform.position) > maxDistance) { break; } if (pieceToCheck.collectingSnake != null) { if (pieceToCheck.collectingSnake == snake) { break; } if (pieceToCheck.collectingSnake != snake) { possibleEnemyTailGroundPieces.Add(pieceToCheck); possibleDirections.Add(-transform.right); break; } } } } if (lastReachedGroundPiece.HasGroundPieceOnSouth()) { for (int i = lastReachedGroundPiece.indexInColumn + 1; i < lastReachedGroundPiece.column.groundPieces.Count; i++) { GroundPiece pieceToCheck = lastReachedGroundPiece.column.groundPieces [i]; if (Vector3.Distance(pieceToCheck.transform.position, transform.position) > maxDistance) { break; } if (pieceToCheck.collectingSnake != null) { if (pieceToCheck.collectingSnake == snake) { break; } if (pieceToCheck.collectingSnake != snake) { possibleEnemyTailGroundPieces.Add(pieceToCheck); possibleDirections.Add(-transform.up); break; } } } } if (lastReachedGroundPiece.HasGroundPieceOnNorth()) { for (int i = lastReachedGroundPiece.indexInColumn - 1; i > 0; i--) { GroundPiece pieceToCheck = lastReachedGroundPiece.column.groundPieces [i]; if (Vector3.Distance(pieceToCheck.transform.position, transform.position) > maxDistance) { break; } if (pieceToCheck.collectingSnake != null) { if (pieceToCheck.collectingSnake == snake) { break; } if (pieceToCheck.collectingSnake != snake) { possibleEnemyTailGroundPieces.Add(pieceToCheck); possibleDirections.Add(transform.up); break; } } } } if (possibleDirections.Count > 0) { int nearestReachableGrounPieceIndex = 0; for (int i = 0; i > possibleEnemyTailGroundPieces.Count; i++) { if (Vector3.Distance(possibleEnemyTailGroundPieces [i].transform.position, transform.position) < Vector3.Distance(possibleEnemyTailGroundPieces [nearestReachableGrounPieceIndex].transform.position, transform.position)) { nearestReachableGrounPieceIndex = i; } } directionToReachEnemySnakeTail = possibleDirections [nearestReachableGrounPieceIndex]; enemyTailGroundPieceToReach = possibleEnemyTailGroundPieces [nearestReachableGrounPieceIndex]; return(true); } else { return(false); } }