Hi, I’m having some problems with detecting a transform inside a range.
When i use this script the turret will detect the enemy. But if there are multiple enemies the turret goes crazy because it changes [0] transform gameObject…
Is there a way to detect an enemy and wait before switching the [0] or transform if the enemy leaves the area or gets killed?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyQueue : MonoBehaviour
{
public LayerMask whoisenemy;
public Vector3 turrentPos;
public float range;
public Collider2D[] enemyQueue;
public Transform enemyTransform;
void Update()
{
enemyQueue = Physics2D.OverlapCircleAll(turrentPos, range, whoisenemy);
for (int i = 0; i < enemyQueue.Length; i++)
{
enemyTransform = enemyQueue[0].transform;
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(turrentPos, range);
}
}