• 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 03:23 AM · playerenemy aistopdie

How do I stop the enemy ai from following the player after it dies on unity3d properly in C#

When I shoot the enemy ai the animation plays . But the problem is that the enemy ai follow the player after it dies . Here is the move and attack 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, t$$anonymous$$s.transform.position) < 13)
          {
              Vector3 direction = player.position - t$$anonymous$$s.transform.position;
              direction.y = 0;
              
              t$$anonymous$$s.transform.rotation = Quaternion.Slerp (t$$anonymous$$s.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:44 AM 0
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 "KillAI" 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 : MonoBehaviour {
   public Transform player;
   static Animator anim;
   public NavMeshAgent nav;

       // Call this method when your player shoot & kills your AI
       public void KillAI()
       {
              // 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 <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);             
          }
       }
   }
avatar image Rajeesh_AR · Jul 27, 2016 at 06:51 AM 1
Share

Your issue is not clear yet. But I think that you are not destroying the enemy after it dies. Destroy(gameobject);

Or if you need the dead enemy-ai to be in the screen, and it should not follow, then just off the script of the enemy which is used to follow the player. Or turn off the navMesh of the enemy-ai. You could use GetComponent for this

Hope this will help you.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

56 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

Related Questions

How do i stop player to move when dead 2 Answers

How can I make the EnemyBullet go further than my target? 0 Answers

Edit this script so that the player stops at a distance and attack? 0 Answers

Enemy ai Ignore walls! help! 1 Answer

Stop player cube from going trough the walls 2 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