All Enemies Follow the player after shoot

Hi.

I have a gun and I draw Ray from gun for shoot to enemies. so i want when player is shooting to enemy, enemy follow the player. problem is when I shoot to one enemy, all enemies follow the player. so i need control each enemy navmesh properties separately.

this is my Gun script:

Gun.cs

 public class Gun : MonoBehaviour {
  
      RaycastHit hit;
      public Camera fpsCam;
      public float range = 100f;
      float Health = 100f
      public LayerMask mask;
      
      void Update ()
      {    
          if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range, mask))
          {
             // Do something
          }
      }
  }

and this is my EnemyController code:

EnemyController.cs

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class EnemyController : MonoBehaviour
 {
 
     [HideInInspector] public NavMeshAgent _navmesh;
     [HideInInspector] public Animator anim;
     GameObject player;
     [HideInInspector] public bool AIState;
     [HideInInspector] public int MaxEnemy = 5;
     Gun gun;
 
     // Use this for initialization
     void Start () 
     {
         _navmesh = GetComponent<NavMeshAgent>();
         anim = GetComponent<Animator> ();
         player = GameObject.FindWithTag ("Player");
         gun = GameObject.Find ("AK47").GetComponent<Gun>();
     }
 
     // Update is called once per frame
     void FixedUpdate ()
     {
         float distance = Vector3.Distance (transform.position, player.transform.position);
 
         for (int i = 0; i <= MaxEnemy; i++)
         {
             if (this.GetComponent<Target>().health > 0)
             {
                 if ((AIState == true && distance > 3f) || (gun.hit.transform != null && gun.hit.Transform.name == "Enemy" && distance > 3f))
                 {
                     anim.SetBool("IsWalking", true);
                     _navmesh.SetDestination(player.transform.position);
                     _navmesh.acceleration = 120f;
                 }
                 else if (distance <= 3f)
                 {
                     transform.LookAt(player.transform);
                     anim.SetBool("IsWalking", false);
                     _navmesh.acceleration = 200f;
                 }
             }
             else
             {
                 _navmesh.speed = 0;
                 //_navmesh.acceleration = 200f;
                 anim.SetBool("IsWalking", false);
                 anim.SetTrigger("Dead");
             }
         }
     }
 
 
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "Player")
         {
             AIState = true;
         }
     }
 }

anyone know how can control each navmesh separately?

Thanks.

Hii, so your problem is when you shoot one enemy all start to follow player and you want only those should follow who is been shoot. if this is true then the problem with you is you are setting destination of all enemy at a time through “_navmesh.SetDestination(player.transform.position);” because naveMesh apply to all agent at a time. hope this will solve your problem

Thanks for reply.

But problem is that i don’t know how can check witch enemy get shoot and then control current navmesh.

it fix in this topic.

http://answers.unity3d.com/questions/1398645/all-enemies-follow-the-player-problem.html