public void StartTower() => StartCoroutine(workOfTower()); // Towers StartToWork IEnumerator workOfTower() { //WorldSpace position of tower is calculated Vector3 position = GetComponent <RectTransform>().transform.position; position = Camera.main.ScreenToWorldPoint(position); position -= Vector3.forward * position.z; //Work loop while (true) { //Finding enemie which is enough close to trigger tower Enemy temp = BattleManagement.Enemies.Find(e => { return((e.transform.position - position).magnitude < TriggerDistance); }); //Attacking this enemy while it is within range while (temp != null && (temp.transform.position - position).magnitude < TriggerDistance) { BattleManagement.Attack(this, temp, out Reward); ResourceManagement.Gold += Reward; //If reward is higher than zero it means enemy was killed if (Reward > 0) { BattleManagement.EnemiesKilled++; } yield return(new WaitForSeconds(FireRate)); } yield return(null); } }
private void Update() { //Finding vector from current unit to next Checkpoint Direction = (CheckPoint.position - transform.position); //Moving towards CheckPoint transform.position += Direction.normalized * speed * Time.deltaTime; //Each CheckPoint hold number of children which are next CheckPoints //If Distance is lower than trigger distance next checkpoint is selected randomly //otherwise it is final checkpoint and the Ultimate Target //Ultimate target is inside of BattleUnit script as static field and each unit "knows" it if (Direction.magnitude < TriggerDistance) { if (CheckPoint.childCount > 0) { CheckPoint = CheckPoint.GetChild(Random.Range(0, CheckPoint.childCount)); } else { BattleManagement.Attack(this, UltimateTarget, out int trash); OnDestroyed(); } } }