-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocateEnemy.cs
35 lines (25 loc) · 1.01 KB
/
LocateEnemy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LocateEnemy : MonoBehaviour
{
public GameObject target;
void Start()
{
}
void Update()
{
FindClosestEnemy();
}
private void FindClosestEnemy() {
target = null;
float closestDistance = gameObject.GetComponent<Shooting>().range;
for (int targetedIndex = 0; targetedIndex < gameObject.GetComponent<levelsCode>().baseEnemiesLength; targetedIndex++) {
float distance = Vector3.Distance(gameObject.GetComponent<levelsCode>().enemies[targetedIndex].transform.position, gameObject.transform.position);
if (distance < closestDistance && gameObject.GetComponent<levelsCode>().enemies[targetedIndex].activeInHierarchy == true) {
closestDistance = distance;
target = gameObject.GetComponent<levelsCode>().enemies[targetedIndex];
}
}
}
}