- Home /
 
 
               Question by 
               dromar · Jul 22, 2017 at 04:09 PM · 
                playerplayer movementfollow  
              
 
              how i can make player 2 go to follow my player again
i have a game that i want player 2 follow player1 and shoot to our same enemy and it works but after the enemy has died it doesnot follow player again it state is follow player and it go to the follow function but it does not move i donot know what is problem , any help 
here is my code : using UnityEngine; using System.Collections; using System.Collections.Generic; using System .Linq ;
[RequireComponent(typeof(UnityEngine.AI.NavMeshAgent))] [RequireComponent(typeof(ThirdPersonCharacter))] public class AICharacterControl : MonoBehaviour {
 public UnityEngine.AI.NavMeshAgent agent { get; private set; }                 // the navmesh agent required for the path finding
 public ThirdPersonCharacter character { get; private set; }     // the character we are controlling
 public Transform target;                                        // target to aim for
 public float targetChangeTolerance = 1;                            // distance to target before target can be changed
 Vector3 targetPos;
 Animator anim;
 WeaponManager weaponManager;
 
 public float attackRate=4f;
 public float attackTimer;
 
 public float range=1000f;
 
 public float  floatInFrontOfWall = 0.001f;
 public float amount =10;
 public Transform spawn;
 
 public float rotationSpeed  = 12.0f;
 public GameObject  Target;                                        // target to aim for
 
 public Camera  myCam;
 private Plane[] planes;
 public Collider EnemyColl;
 
 
 IKLimb_BrunoFerreira IKLimb_BrunoFerreira;
 
 public GameObject EnemyToAttack;
 
 public  bool  IsAttacking  =false ;
 
 public bool CanSeeTarget  ;
 public List<Collider  > colliders =new List<Collider >(); 
 public enum AIstate
 {
     followPlayer,Attack
 }
 public AIstate aiState;
 // Use this for initialization
 void Start () {
     agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
     character = GetComponent<ThirdPersonCharacter>();
     agent=GetComponentInChildren<UnityEngine.AI.NavMeshAgent >();   
     anim=GetComponent<Animator  >();   
     weaponManager =GetComponent<WeaponManager >();   
     IKLimb_BrunoFerreira = GetComponent<IKLimb_BrunoFerreira>();
     agent .updatePosition = true;
     agent .updateRotation = true;
     aiState = AIstate.followPlayer;
 }
 // Update is called once per frame
 void Update () {
     switch (aiState)
     {
     case AIstate.followPlayer  :
         FollowPlayer ();
         break ;
     case AIstate.Attack :
         Attack();
         break ;
     }        
     DecideState (); 
     if (EnemyToAttack.gameObject.activeSelf == false) {
         IsAttacking = false;
         CanSeeTarget = false;
         aiState = AIstate.followPlayer;
         //FollowPlayer ();
     }
     }
 void DecideState () {
     
 planes = GeometryUtility.CalculateFrustumPlanes(myCam);
     if (colliders.Count > 0) {
         foreach (Collider collide in colliders) {
             if (GeometryUtility.TestPlanesAABB (planes, collide.bounds)) {    
             
                 Vector3 direction = collide.gameObject.transform.position - transform.position;
                 float angle = Vector3.Angle (direction, transform.forward); 
                 if (angle < 110f * 0.5f) {
                     EnemyToAttack = collide.gameObject;
                     Debug.Log ("looking at player");
                     CanSeeTarget = true;
                     aiState = AIstate.Attack;
                     Attack ();
                 }
             }
         }
     } 
      
     else 
     
     {
         aiState = AIstate.followPlayer;
     }
 }
 void FollowPlayer(){
     
     if (CanSeeTarget == false) {
         IsAttacking = false;
         // update the agents posiiton 
         agent.transform.position = transform.position;
         // use the values to move the character
         character.Move (agent.desiredVelocity, false, false, false, false, false, targetPos);
     }
         if (target != null) {
             //Debug.Log ("3");  
             // update the progress if the character has made it to the previous target
         //    if ((target.position - targetPos).magnitude > targetChangeTolerance)
         //{
                 //
                 targetPos = target.position + Vector3.up;
                 agent.SetDestination (targetPos);
         //    }
             Debug.Log ("1");
         } 
         else
         
         {
             // We still need to call the character's move function, but we send zeroed input as the move param.
             character.Move (Vector3.zero, false, false, false, false, false, transform.position + transform.forward * 50);
             Debug.Log ("mov at player");
         }
     //}
 
 }
 void Attack(){
     //CheckForPlayer ();
     //patrol =false ;
     
     
         if (IsAttacking = true) {
             agent.Stop ();
             anim.SetFloat ("Turn", 0); 
             anim.SetFloat ("Forward", 0); 
             weaponManager.aim = true;
             //anim.SetBool  ("Aim",true ); 
             character.Move (Vector3.zero, true, false, false, false, false, Vector3.zero);
             Vector3 direction = EnemyToAttack.transform.position - transform.position;
             float angle = Vector3.Angle (direction, transform.forward);  
             if (angle < 110 * 0.5f) {
                 transform.LookAt (EnemyToAttack.transform.position);
                 //transform.localEulerAngles   = Vector3 (0, transform.eulerAngles.y, 0);
                 // enemy .rotation = Quaternion.Slerp(player .rotation, Quaternion.LookRotation(player .position - enemy .position), rotationSpeed * Time.deltaTime);
             }
             Vector3 velocity = agent.desiredVelocity * 3f;
             character.Move (velocity, true, false, false, false, false, EnemyToAttack.transform.position);
             anim.SetBool ("Aim", true); 
             anim.SetBool ("Fire", true); 
             attackTimer += Time.deltaTime;
             if (attackTimer > attackRate) {    
                 //ShootRay();
                 attackTimer = 0;
             }
         }
     }
 //}
 void OnAnimatorIk ()
 {
     if (EnemyToAttack) {
         anim.SetLookAtWeight (1, 0.8f, 1, 1, 1);
         anim.SetLookAtPosition (EnemyToAttack.transform.position);
     }
     else
     {
         anim.SetLookAtWeight(0);
     }
 }
 //public void SetTarget(Transform target)
 //{
     //this.target = target;
 //}
 
               }
               Comment
              
 
               
              Your answer