Почему туррель убивает самого ближайщеговрага а потом не стреляет других пока они не станут ближе последнего?
void LockTarget()
{
if (target != null) return;
GameObject[] zombies = GameObject.FindGameObjectsWithTag("Zombie");
foreach (GameObject zombie in zombies)
{
octree.Add(zombie, zombie.GetComponent<Collider>().bounds);
}
closestDistanceSqr = maxDistance * maxDistance;
GameObject closestTarget = null;
List<GameObject> nearbyZombies = new List<GameObject>();
octree.GetColliding(nearbyZombies, new Bounds(turret.position, new Vector3(maxDistance, maxDistance, maxDistance)*2));
nearbyZombies.RemoveAll(x => x == null);
foreach (GameObject zombie in nearbyZombies)
{
if (zombie == null)
{
nearbyZombies.RemoveAll(x => x == null);
octree.Remove(zombie);
closestDistanceSqr = maxDistance * maxDistance;
continue;
}
float distanceSqr = (zombie.transform.position - turret.position).sqrMagnitude;
if (distanceSqr < closestDistanceSqr)
{
closestDistanceSqr = distanceSqr;
closestTarget = zombie;
}
}
if (closestTarget != null)
{
target = closestTarget;
}
}
это стрельба (здесь тоже есть кусок)
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(gun.position, -gun.forward, out hit, maxDistance, layerMask2))
{
if (hit.transform.gameObject == target)
{
hit.transform.TryGetComponent(out Zombie zombie);
if ((timer += Time.deltaTime) > delay)
{
if (!reload)
{
rockets[num].launch = true;
rockets[num].transform.parent = null;
num++;
if (num >= rockets.Length) reload = true;
Instantiate(shootSound, gun);
timer = 0;
}
}
if (!zombie.isAlive)
{
closestDistanceSqr = maxDistance * maxDistance;
target = null;
}
}
else
{
target = null;
}
}
else
{
target = null;
}
}