public Transform GetMoveTarget(Transform chess) { IList mapList = new List <Transform>(); IList selfTerritory = new List <Transform>(); IList closeList = new List <Transform>(); Transform targetMap = null; CharacterSelect chessSel = chess.GetComponent <CharacterSelect>(); currentSelect.updateMapSteps(); Transform currentPos = chessSel.getMapPosition(); chessSel.findMoveRange(currentPos, 0, chess.GetComponent <CharacterProperty>().BuffMoveRange); foreach (Transform map in chessSel.MoveRangeList) { if (!MapHelper.IsMapOccupied(map)) { mapList.Add(map); } } chessSel.MoveRangeList.Clear(); foreach (Transform map in currentRC.PlayerBTerritory) { if (!MapHelper.IsMapOccupied(map)) { selfTerritory.Add(map); } } Transform closestOne = Camera.main.GetComponent <MoveCharacter>().GetClosetChess(chess); closeList = MapHelper.GetClosestMaps(closestOne, mapList); //print("The Closest One: "+closestOne); if (closeList.Count == 1) { targetMap = closeList[0] as Transform; } else if (closeList.Count > 1) { Transform[] pPos = new Transform[selfTerritory.Count]; selfTerritory.CopyTo(pPos, 0); Transform[] closestPos = new Transform[closeList.Count]; closeList.CopyTo(closestPos, 0); IEnumerable <Transform> bothPos = pPos.Intersect(closestPos); Transform[] bothArray = bothPos.ToArray(); if (bothArray.Length == 1) { targetMap = bothArray[0] as Transform; } else if (bothArray.Length > 1) { int rnd = Random.Range(0, bothArray.Length - 1); targetMap = bothArray[rnd] as Transform; } else if (bothArray.Length == 0) { int rnd = Random.Range(0, closeList.Count - 1); targetMap = closeList[rnd] as Transform; } } return(targetMap); }