- Home /
Making NavMesh agents individual if they are running on the same script
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?
Answer by Naiveca · Sep 22, 2021 at 04:30 PM
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.
Your answer
Follow this Question
Related Questions
NavMesh Agent rotation problem 3 Answers
How to queue NavMeshAgents on entering a tile in Unity (based on path distance to tile)? 0 Answers
Making NavMesh areas? 0 Answers
Why Nav Mesh Agent has been baked intermittently on 1 straight component? 0 Answers
How to generate Nav Mesh floor in a procedural generated maze? 3 Answers