public Transform findEnemyToAttack() { float distanceToClosestEnemy = Mathf.Infinity; float maxDistanceToAttackDamagedEnemies = 10f; Transform closestEnemy = null; Transform mostDamagedEnemyNearby = null; float minHealth = 100; GameObject[] allEnemies = GameObject.FindGameObjectsWithTag("Player"); foreach (GameObject currentEnemy in allEnemies) { float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude; ShipDestruction sd = currentEnemy.GetComponent <ShipDestruction>(); float health = sd.health; //Find closest Enemy if (distanceToEnemy < distanceToClosestEnemy) { distanceToClosestEnemy = distanceToEnemy; closestEnemy = currentEnemy.transform; } //Find most Damaged Enemy within a Radius of 10f if (health < minHealth && distanceToEnemy < maxDistanceToAttackDamagedEnemies) { minHealth = health; mostDamagedEnemyNearby = currentEnemy.transform; } } return(mostDamagedEnemyNearby ? mostDamagedEnemyNearby : closestEnemy); }
// Start is called before the first frame update void Start() { if (GlobalVariables.local) { enabled = false; } else { dest = gameObject.GetComponent <ShipDestruction>(); } }
private void provideItemToShip(GameObject ship) { FindObjectOfType <AudioManager>().Play("collect_item"); ShipDestruction shipDestruction = ship.GetComponent <ShipDestruction>(); if (GlobalVariables.singlePlayer && ship.name.StartsWith("Ship_Mars")) { AIBehaviour ai = ship.GetComponent <AIBehaviour>(); switch (itemType) { case ItemType.MISSILES: ai.missileAmount += 3; this.collectInfo = "+3 MISSILES!!"; break; case ItemType.HEALTH: shipDestruction.health = 100; this.collectInfo = "FULL HEALTH!!"; break; case ItemType.LASER: ai.laserAmount += 1; this.collectInfo = "+1 LASER!!"; break; } } else { Shoot shoot = ship.GetComponent <Shoot>(); switch (itemType) { case ItemType.MISSILES: shoot.missileAmount += 3; this.collectInfo = "+3 MISSILES!!"; break; case ItemType.HEALTH: shipDestruction.health = 100; this.collectInfo = "FULL HEALTH!!"; break; case ItemType.LASER: shoot.laserAmount += 1; this.collectInfo = "+1 LASER!!"; break; } } }