void Start() { foundEnemyEvent.AddListener(FoundEnemy); target = GameManager.instance.player.transform; agent = GetComponent <NavMeshAgent>(); thisFaction = GetComponent <FactionType>().faction; }
// Update is called once per frame void Update() { thisFaction = GetComponent <FactionType>().faction; Collider[] colliders = Physics.OverlapSphere(transform.position, lookRadius); closestEnemy = Mathf.Infinity; foreach (Collider hit in colliders) { if (hit != null && hit.GetComponent <Enemy>() || hit.GetComponent <PlayerManager>()) { FactionType tempFactionType = hit.transform.GetComponent <FactionType>(); if (tempFactionType) { //faction = tempFactionType.faction; if (thisFaction != hit.GetComponent <FactionType>().faction) { float distToEnemy = Vector3.Distance(transform.position, hit.transform.position); if (distToEnemy < closestEnemy) { closestEnemy = distToEnemy; bestTarget = hit.transform; } target = bestTarget; //Debug.Log("bestTarget"); } } } } float distance = target ? Vector3.Distance(target.position, transform.position) : Mathf.Infinity; if (distance <= lookRadius) { foundEnemyEvent.Invoke(); } }
public void SearchForFactions() { factionWaveManager = factionSpawn.GetComponent <FactionWaveManager>(); enemies = factionWaveManager.enemies; closestEnemy = Mathf.Infinity; foreach (GameObject enemy in enemies) { Collider[] colliders = Physics.OverlapSphere(enemy.transform.position, searchRadius); foreach (Collider hit in colliders) { if (hit != null) { FactionType tempFactionType = hit.transform.GetComponent <FactionType>(); if (tempFactionType) { faction = tempFactionType.faction; if (faction != FactionType.Faction.None && faction != enemy.GetComponent <FactionType>().faction) { Enemy enemyHit = hit.transform.GetComponent <Enemy>(); float distToEnemy = Vector3.Distance(hit.transform.position, enemy.transform.position); if (enemyHit != null && distToEnemy < closestEnemy) { closestEnemy = distToEnemy; bestTarget = enemyHit.transform; } enemy.GetComponent <EnemyAI>().target = bestTarget; // Debug.Log("TargetFound"); } } } } } }
public void Initialise(List <GameObject> _enemies, FactionType.Faction _faction) { enemies = _enemies; faction = _faction; }
private void Start() { thisFaction = this.GetComponent <FactionType>().faction; }