private IEnumerator MoveUnitAway(Unit unit, Unit awayFrom) { Node farthestNodeFromUnit = null; float farthestDist = 0; Node[] movableNodes = Pathfinding.instance.GetNodesMinMaxRange(unit.transform.position, unit.GetCanFly(), 1, (int)unit.GetMovementSpeed()).ToArray(); if (movableNodes.Length < 1) { yield break; } for (int i = 0; i < movableNodes.Length; i++) { Node currentNode = movableNodes[i]; float currentDist = Vector3.Distance(awayFrom.transform.position, currentNode.worldPosition); if (currentDist > farthestDist && currentNode.canWalkHere && !currentNode.unitInThisNode) { farthestNodeFromUnit = currentNode; farthestDist = currentDist; } } yield return(unit.AIFollowPath(Pathfinding.instance.AIFindPath(unit.transform.position, farthestNodeFromUnit.worldPosition, unit.GetCanFly(), unit.GetUnitPlayerID()))); PlayerMana--; }
private IEnumerator MoveUnitTowards(Unit unit, Vector3 targetLocation) { Node closestNodeToTarget = null; float closetDist = Mathf.Infinity; Node[] movableNodes = Pathfinding.instance.GetNodesMinMaxRange(unit.transform.position, unit.GetCanFly(), 1, unit.GetMovementSpeedLeft()).ToArray(); if (movableNodes.Length < 1) { yield break; } for (int i = 0; i < movableNodes.Length; i++) { Node currentNode = movableNodes[i]; float currentDist = Vector3.Distance(targetLocation, currentNode.worldPosition); if (currentDist < closetDist && (unit.GetCanFly() || currentNode.canWalkHere) && !currentNode.unitInThisNode) { closestNodeToTarget = currentNode; closetDist = currentDist; } } yield return(unit.AIFollowPath(Pathfinding.instance.AIFindPath(unit.transform.position, closestNodeToTarget.worldPosition, unit.GetCanFly(), unit.GetUnitPlayerID()))); PlayerMana--; }