Question by
megaxlf · Apr 07, 2019 at 03:43 AM ·
raycastairaycasthitforeach
Foreach working very strange
I'm doing an entire AI game... I want them to look at the others AI and shoot. to help my life I did a list with all the AI enemies and a foreach to make a raycast and to Instantiate the bullets... everything is working, but only with one enemy.
If the enemy's hiden behind something as long some other target is at sign of the principal, the principal AI keep shooting at this first enemy, he just change the target if I destroy this first enemy. I want him to shoot at an enemy, if the enemy is behind something he change the target.
I think is something of how the foreach works, but I don't know how to fix.
foreach (Transform item in _enemy)
{
if (item != null)
{
_directionEnemies = new Vector3(item.transform.position.x - transform.position.x, item.transform.position.y - transform.position.y, item.transform.position.z - transform.position.z);
if (Physics.Raycast(transform.position, _directionEnemies, out _hit, 80f) && _hit.transform == item.transform && Time.time >=_nextShoot)
{
_nextShoot= Time.time + 1f / _cadence;
Instantiate(_bullet, transform.position, Quaternion.identity);
}
Debug.DrawLine(transform.position, item.transform.position, Color.red);
}
}
Comment