I’ve made the basics for an RTS game. I’ve ran in to a problem where, if I have 2 of the same type of unit that are obviously running the same script, they both start moving toward an enemy if I attack it with the other one. “agent.SetDestination(Enemy.transform.position);” gets called on both of the units. How would I make it so the script handles both agents individually?
I don’t understand what I did, but my Unit is using a sphere collider to check if an Enemy Unit comes in range and attacks it. Both of my Units running the same script would attack the enemy if the other Units collider was triggered. I changed this:
private void OnTriggerEnter(Collider other)
if (other.tag == "EnemyUnit")
{
Enemy = GameObject.FindObjectsWithTag("EnemyUnit")
if (Enemy != null && IsMoving == false)
{
FollowingEnemy = true; //this invokes the attack
}
}
}
To this:
private void OnTriggerEnter(Collider other)
{
if (other.tag == "EnemyUnit")
{
Enemy = other.gameObject;
if (Enemy != null && IsMoving == false)
{
FollowingEnemy = true;
}
}
}
And now only the one that’s sphere collider was triggered starts to move and attack. No idea what changed.