Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. public Transform FindClosestEnemy() //ENEMY TARGETS A CLOSEST TOWER
  2. {
  3.  
  4. List<MonsterController> totalEnemies = ListMaster.instance.enemies;
  5.  
  6.  
  7. if (totalEnemies.Count == 0)
  8. return null;
  9.  
  10.  
  11. float shortestDistance = Mathf.Infinity;
  12.  
  13.  
  14. MonsterController closestEnemy = null;
  15.  
  16.  
  17. foreach (MonsterController enemy in totalEnemies)
  18. {
  19.  
  20. float distanceToEnemy = MathMaster.GetDistance(enemy.transform.position, transform.position);
  21.  
  22.  
  23. if (distanceToEnemy >= _statControl.GetCurrentFinalRange()) continue; //This prevents us from having targets out of range.
  24. if (distanceToEnemy >= shortestDistance) continue; //Enemy is farther away than our current closest so hes skipped and we repeat everything above.
  25. if (enemy.focusedBy >= enemy.focusedByCap) continue; //If the enemy is at a max focus cap he is skipped and we repeat everything above.
  26.  
  27.  
  28.  
  29. shortestDistance = distanceToEnemy;
  30. closestEnemy = enemy;
  31.  
  32.  
  33. }
  34.  
  35.  
  36. if (closestEnemy)
  37. {
  38.  
  39. closestEnemy.targetedByList.Add(_monsterControl);
  40. closestEnemy.focusedBy++;
  41.  
  42. }
  43.  
  44.  
  45. if (!closestEnemy)
  46. return null;
  47.  
  48. return closestEnemy.transform;
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement