I have AI enemies, which walk to the closest player target and attack, when you enter their sphere trigger collider. This works just fine, until multiple enemies are added.
I have an EnemySight script with the following code:
private void OnTriggerStay(Collider other)
{
if (other.gameObject == player)
{
playerInSight = true;
}
}
And then after one more script which determines the animate state, the following code moves the enemy to the player:
void Walk()
{
if (enemySight.playerOnRight == true && !facingRight)
{
Flip();
}
else if (enemySight.playerOnRight == false && facingRight)
{
Flip();
}
navMeshAgent.speed = enemySpeed;
enemyCurrentSpeed = navMeshAgent.velocity.sqrMagnitude;
navMeshAgent.SetDestination(enemySight.target.transform.position);
navMeshAgent.updateRotation = false;
}
When more than one enemy is in the scene, both enemies react to “PlayerInSIght”. I assume I need to add enemies to a list, and have only the index of an OnTrigger… react, but I don’t know how to go about that.
I’d be happy to post my full project if it would help.