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 importguru88 · Jul 27, 2016 at 04:36 AM · followstopenemyaifollow player

How do I make the enemy ai stop following the player in the scene properly on unity3d

I have the enemy ai following the play and I nav mesh set to stop in the inspector . The problem is the enemy ai is still following the player regardless if its dead or alive . I want the enemy ai to die where its at. I have it to where the enemy ai is checking to see if the player is near. Here is my script :

 using UnityEngine;
  using System.Collections;
  
  public class Enemyai : MonoBehaviour {
  public Transform player;
  static Animator anim;
  public NavMeshAgent nav;               
      void Start () 
      {
              anim = GetComponent<Animator> ();
      }
      
  
      void Update () 
      {
          if (Vector3.Distance(player.position, this.transform.position) < 13)
          {
              Vector3 direction = player.position - this.transform.position;
              direction.y = 0;
              
              this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
              
              anim.SetBool("isIdle",false);
              if(direction.magnitude > 2.6 )
              {
               nav = GetComponent <NavMeshAgent> ();
               nav.SetDestination (player.position);
                   anim.SetBool("isMoving",true);
                  anim.SetBool("isAttack",false);
                  }
              else
              {
                  anim.SetBool("isAttack",true);
                  anim.SetBool("isMoving",false);
                  
              }
          }
         else
         {
             anim.SetBool("isIdle",true);
             anim.SetBool("isMoving",false);
             anim.SetBool("isAttack",false);
             
         }
      
      
      }
  }
  


Comment
Add comment · Show 2
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
avatar image piotte-j · Jul 27, 2016 at 06:42 AM 1
Share

Hey mate ! I'm not familiar with navmeshes but I would recommend the following. First your need a way to declare your AI dead. For example, you can add a "$$anonymous$$illAI" method to your script and call it when your AI is getting shot by your player.

Then I would try two things to make your AI stop following your player :

  • First, you can try to disable the navmesh script.

  • Second, you can set the destination to your AI current position.

By the way, you might wanna disable the AI when your enemy dies (I suppose it makes no sense for you that some AI keeps being computed for dead enemies?)

Here is how you can achieve that :

  using UnityEngine;
  using System.Collections;
   
   public class Enemyai : $$anonymous$$onoBehaviour {
   public Transform player;
   static Animator anim;
   public Nav$$anonymous$$eshAgent nav;

       // Call this method when your player shoot & kills your AI
       public void $$anonymous$$illAI()
       {
              // 1st solution
              nav.enabled = false;

              // 2nd solution
              nav.SetDestination (nav.position);

              // If you want to stop the AI when the enemy dies
              enabled = false;
       }
 
       void Start () 
       {
               anim = GetComponent<Animator> ();
       }
       
   
       void Update () 
       {
           if (Vector3.Distance(player.position, this.transform.position) < 13)
           {
               Vector3 direction = player.position - this.transform.position;
               direction.y = 0;
               
               this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
               
               anim.SetBool("isIdle",false);
               if(direction.magnitude > 2.6 )
               {
                nav = GetComponent <Nav$$anonymous$$eshAgent> ();
                nav.SetDestination (player.position);
                    anim.SetBool("is$$anonymous$$oving",true);
                   anim.SetBool("isAttack",false);
                   }
               else
               {
                   anim.SetBool("isAttack",true);
                   anim.SetBool("is$$anonymous$$oving",false);                  
               }
           }
          else
          {
              anim.SetBool("isIdle",true);
              anim.SetBool("is$$anonymous$$oving",false);
              anim.SetBool("isAttack",false);             
          }
       }
   }
avatar image importguru88 piotte-j · Jul 27, 2016 at 09:26 AM 0
Share

I tried your script and I am getting errors.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by rocket350 · Jul 27, 2016 at 11:38 AM

Ok, I misunderstood you in your previous question.
If you want your Enemy GameObject to continue existing even after death, but want it to stop moving, add the following function in you original EnemyAI script:

 public void Die(){
     //Disable all animations on the enemy AI
     anim.SetBool("isIdle",true);
     anim.SetBool("isMoving",false);
     anim.SetBool("isAttack",false);
     //Stop the navMeshAgent
     nav.Stop();
     //Remove the AI script (the enemy is dead now)
     Destroy(this);
 }

If you want to completely destroy the gameobject, add this function instead

 public void Die(){
     //Destroy the enemy gameobject
     Destroy(gameObject);
     
 }

In your enemy AI Health script you posted, add the following lines at the end of DieLocal()

 GetComponent<Enemyai>().Die();
Comment
Add comment · Show 9 · Share
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
avatar image rocket350 · Jul 27, 2016 at 11:44 AM 0
Share

So this is what your Enemyai script would look like: using UnityEngine; using System.Collections;

   public class Enemyai : $$anonymous$$onoBehaviour {
   public Transform player;
   static Animator anim;
   public Nav$$anonymous$$eshAgent nav;               
       void Start () 
       {
               anim = GetComponent<Animator> ();
       }

       public void Die(){
            //Disable all animations on the enemy AI
            anim.SetBool("isIdle",true);
            anim.SetBool("is$$anonymous$$oving",false);
            anim.SetBool("isAttack",false);
           //Stop the nav$$anonymous$$eshAgent
            nav.Stop();
            //Remove the AI script (the enemy is dead now)
            Destroy(this);
       }
   
       void Update () 
       {
           if (Vector3.Distance(player.position, this.transform.position) < 13)
           {
               Vector3 direction = player.position - this.transform.position;
               direction.y = 0;
               
               this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
               
               anim.SetBool("isIdle",false);
               if(direction.magnitude > 2.6 )
               {
                nav = GetComponent <Nav$$anonymous$$eshAgent> ();
                nav.SetDestination (player.position);
                    anim.SetBool("is$$anonymous$$oving",true);
                   anim.SetBool("isAttack",false);
                   }
               else
               {
                   anim.SetBool("isAttack",true);
                   anim.SetBool("is$$anonymous$$oving",false);
                   
               }
           }
          else
          {
              anim.SetBool("isIdle",true);
              anim.SetBool("is$$anonymous$$oving",false);
              anim.SetBool("isAttack",false);
              
          }
       
       
       }
   }
avatar image rocket350 · Jul 27, 2016 at 11:44 AM 0
Share

And your DieLocal function in your AI Health Script

 private void  DieLocal(Vector3 position, Vector3 force) {
              // Notify those interested.
              EventHandler.ExecuteEvent(m_GameObject, "OnDeath");
              EventHandler.ExecuteEvent<Vector3, Vector3>(m_GameObject, "OnDeathDetails", force, position);
  
              // Spawn any objects on death, such as an explosion if the object is an explosive barrell.
              for (int i = 0; i < m_SpawnedObjectsOnDeath.Length; ++i) {
                  ObjectPool.Instantiate(m_SpawnedObjectsOnDeath[i], transform.position, transform.rotation);
              }
  
              // Destroy any objects on death. The objects will be placed back in the object pool if they were created within it otherwise the object will be destroyed.
              for (int i = 0; i < m_DestroyedObjectsOnDeath.Length; ++i) {
                  if (ObjectPool.SpawnedWithPool(m_DestroyedObjectsOnDeath[i])) {
                      ObjectPool.Destroy(m_DestroyedObjectsOnDeath[i]);
  
                  } else {
                      Object.Destroy(m_DestroyedObjectsOnDeath[i]);
                  }
              }
  
              // Deactivate the object if requested.
             
              
               if (m_DeactivateOnDeath) {
                  Scheduler.Schedule(m_DeactivateOnDeathDelay, Deactivate);
                      GetComponent<AudioSource>().Play();
                          anim.SetTrigger("isDead");
                          
                          }
               GetComponent<Enemyai>().Die();        
 }
avatar image importguru88 rocket350 · Jul 27, 2016 at 12:08 PM 0
Share

I can make the project into a zip file and download it and check it . I've gotten an error when I did your script :

 private void  DieLocal(Vector3 position, Vector3 force) {
               // Notify those interested.
               EventHandler.ExecuteEvent(m_GameObject, "OnDeath");
               EventHandler.ExecuteEvent<Vector3, Vector3>(m_GameObject, "OnDeathDetails", force, position);
   
               // Spawn any objects on death, such as an explosion if the object is an explosive barrell.
               for (int i = 0; i < m_SpawnedObjectsOnDeath.Length; ++i) {
                   ObjectPool.Instantiate(m_SpawnedObjectsOnDeath[i], transform.position, transform.rotation);
               }
   
               // Destroy any objects on death. The objects will be placed back in the object pool if they were created within it otherwise the object will be destroyed.
               for (int i = 0; i < m_DestroyedObjectsOnDeath.Length; ++i) {
                   if (ObjectPool.SpawnedWithPool(m_DestroyedObjectsOnDeath[i])) {
                       ObjectPool.Destroy(m_DestroyedObjectsOnDeath[i]);
   
                   } else {
                       Object.Destroy(m_DestroyedObjectsOnDeath[i]);
                   }
               }
   
               // Deactivate the object if requested.
              
               
                if (m_DeactivateOnDeath) {
                   Scheduler.Schedule(m_DeactivateOnDeathDelay, Deactivate);
                       GetComponent<AudioSource>().Play();
                           anim.SetTrigger("isDead");
                           
                           }
                GetComponent<Enemyai>().Die();        
  }

avatar image importguru88 importguru88 · Jul 27, 2016 at 12:09 PM 0
Share

you can download the project.

Show more comments
avatar image importguru88 rocket350 · Jul 27, 2016 at 01:49 PM 0
Share

Here is the link : http://www.mediafire.com/download/nrlf09jf4a8cezt/New+Unity+Project+1.zip

avatar image importguru88 rocket350 · Jul 27, 2016 at 08:14 PM 0
Share

Did you download project from the link ?

avatar image rocket350 importguru88 · Jul 28, 2016 at 03:42 PM 0
Share

O$$anonymous$$ I downloaded the project. Your problem is a simple one: The Die() function is called Stop() ins$$anonymous$$d. So what you need to do is this: IN your EnemyAI script, rename Stop() to Die(). That's all!
PS: Nice scene!

Cheers :)

Show more comments

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

52 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

Related Questions

AI Enemy Follow Player 2 Answers

Enemy is not stopping following the player 2 Answers

Following AI - Similar to Snake Game 2 Answers

Make an NPC follow player? 1 Answer

Having a friendly character follow player 0 Answers


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