Multiple AI enemies: Using OnTrigger for "line of sight" activates all enemies at once
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.
try
private bool playerInSight ;
// ins$$anonymous$$d of
public bool playerInSight ;
Answer by b_schwartz · Sep 01, 2017 at 02:48 AM
Problem solved!
Static animation variables was the issue. Making them public has cluttered my editor a bit, but each enemy now acts individually.
Edit: Private works as well (duh). Editor decluttered.
Answer by ppg · Aug 31, 2017 at 08:40 PM
this.playerInSight = true;
you don't necessarily need a list for each object to react individually so far each have their own script attached to the game object it should work your issue might be with something else
I fixed it :D
The issue wasn't any of this, it was simply that my animator state variables were static, ins$$anonymous$$d of public, so both enemies were trying to share the same animation.
O$$anonymous$$ thought it might have something to do with how the var are reference ie public /private but it was static.
Hmm, well that solved part of the problem. I put the move code inside that conditional, and now the second enemy doesn't react at all, and the proper enemy animates to walk, but it doesn't move, which is what this code is actually supposed to execute.
I will keep playing, thanks!
O$$anonymous$$ keep in $$anonymous$$d if using trigger function you have less steps than a update functions that runs continuously CODES $$anonymous$$OST TI$$anonymous$$ES WILL REACT DIFFERENTLY IN THE TRIGGER FUNCTION BECAUSE ITS $$anonymous$$EANT TO RUN ONCE ON TRIGGER
make sure you use a private bool also try targeting the instance object with the (this.object) syntax
check other questions related to your issue i have answered a few here today CHEC$$anonymous$$ QUESTION LISTS FOR TODAY good luck
Thanks for the tips :) I have indeed found a few questions related to this, but I'll take another look. So now I'm thinking I probably don't need to bother with a list, and just get everything under a "this" statement. But I do get that Triggers only run when they're triggered.