Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

75 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

Issue with player landing on moving platform - Infinite Runner 4 Answers

Player falling through the floor 1 Answer

How do I jump? 0 Answers

Transform player from one position to another after completing a goal 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges