tmoneygames

OnMissleShoot

Nov 23rd, 2019 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | Source Code | 0 0
  1. private void OnMissileShot()
  2.     {
  3.         float closestDistance = 9999;
  4.         Enemy bestTarget = null;
  5.  
  6.         // Iterate through the scenes list of enemies and check which is the closest to us.
  7.         foreach (Enemy target in GameObject.FindObjectsOfType<Enemy>())
  8.         {
  9.             // check if the target is already tracked
  10.             if (!target.OnCheckMissileTracked())
  11.             {
  12.                 Vector2 distance = target.transform.position - transform.position;
  13.                 if (distance.magnitude < closestDistance)
  14.                 {
  15.                     bestTarget = target;
  16.                 }
  17.             }
  18.         }
  19.  
  20.         if (bestTarget != null)
  21.         {
  22.             bestTarget.OnMissileTracked();
  23.             Instantiate(_missle, transform.position, Quaternion.identity).GetComponent<Missile_Behaviour>().OnSetTarget(bestTarget.transform);
  24.         }
  25.  
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment