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 /
  • Help Room /
This question was closed May 29, 2021 at 03:56 PM by thenoodlegrush for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by thenoodlegrush · Aug 12, 2017 at 06:29 PM · unity 5playerenemyenemyaifollow player

How to stop enemy following the player

Hi guys! Can someone help me please with my script? How to stop enemy when player is on some distance from him? And how to make him going when player is near enemy? Here is the code and now enemy stop only when the player is dead. But if the player is alive - he is following the player all the time. How to change it?

Thx for attention!

 using UnityEngine;
 using System.Collections;
 
 public class EnemyMovement : MonoBehaviour
 {
     Transform player;
     PlayerHealth playerHealth;
     EnemyHealth enemyHealth;
     UnityEngine.AI.NavMeshAgent nav;
 
 
     void Awake ()
     {
         player = GameObject.FindGameObjectWithTag ("Player").transform;
         playerHealth = player.GetComponent <PlayerHealth> ();
         enemyHealth = GetComponent <EnemyHealth> ();
         nav = GetComponent <UnityEngine.AI.NavMeshAgent> ();
     }
 
 
     void Update ()
     {
         if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
         {
             nav.SetDestination (player.position);
         }
         else
         {
            nav.enabled = false;
         }
             
     }
 }
 
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

2 Replies

  • Sort: 
avatar image
0

Answer by Onk3y · Aug 14, 2017 at 10:46 AM

Instead of using

nav.enabled = false

try to use

nav.ResetPath()

This will clear the Destination of your NavMeshAgent. If you want your agent to follow the Player again just call NavMeshAgent.SetDestination(player.position) again. nav.enabled is used for (de-)activating the component and that is not what you want to do. Hope this works for you.

Comment
Add comment · Show 6 · 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 thenoodlegrush · Aug 14, 2017 at 03:33 PM 1
Share

thx for reply, but it`s not working =(((

maybe i have wrote something wrong?

 using UnityEngine;
 using System.Collections;
 
 public class Enemy$$anonymous$$ovement : $$anonymous$$onoBehaviour
 {
     Transform player;
     PlayerHealth playerHealth;
     EnemyHealth enemyHealth;
     UnityEngine.AI.Nav$$anonymous$$eshAgent nav;
 
 
     void Awake ()
     {
         player = GameObject.FindGameObjectWithTag ("Player").transform;
         playerHealth = player.GetComponent <PlayerHealth> ();
         enemyHealth = GetComponent <EnemyHealth> ();
         nav = GetComponent <UnityEngine.AI.Nav$$anonymous$$eshAgent> ();
     }
 
 
     void Update ()
     {
         if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
         {
             nav.SetDestination (player.position);
         }
         else
         {
             nav.ResetPath(); 
             nav.SetDestination (player.position);
 
         }
             
     }
 }
 
avatar image Onk3y thenoodlegrush · Aug 14, 2017 at 03:53 PM 0
Share

@thenoodlegrush Actually there is a small mistake in your code. The line of code after "nav.ResetPath();" will screw everything up because you immediately set a new destination after you've deleted your old one. Just delete the line "nav.SetDestination (player.position);" after the line "nav.ResetPath()" and you're good to go (delete line 30). It might have been a bit unclear in my original answer but call "nav.SetDestination (player.position);" to follow the player and call "nav.ResetPath()" if you want to stop following the player.

avatar image thenoodlegrush Onk3y · Aug 14, 2017 at 04:02 PM 0
Share

i have deleted this line, but enemy still chasing the player, even if the player is on really big distance - he still following the player. how to setup correctly the distance after which he will be stopping? and when player will back on this distance - he will following player again?

Show more comments
avatar image
0

Answer by Tomikip · Aug 03, 2020 at 11:25 AM

You can try this if (Vector2.Distance(transform.position, target.position) > stoppingDistance) { transform.position = Vector2.MoveTowards(transform.position, target.position, MoveSpeed Time.deltaTime); } else if (Vector2.Distance(transform.position, target.position) < stoppingDistance && (Vector2.Distance(transform.position, target.position) > retreatDistance)) { transform.position = this.transform.position; } else if ((Vector2.Distance(transform.position, target.position) < retreatDistance)) { transform.position = Vector2.MoveTowards(transform.position, target.position, -MoveSpeed Time.deltaTime); }

In the update method (my game is in 2d so you will probably need Vector3 i dont really know ^^)

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

188 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 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 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

Enemy doesn't follow player clone,Enemy doesn't follow the player clone 0 Answers

I have 2 enemies connected to one script but when one enemy dies using destroy(gameObject); the other enemy is still present but my character doesn't recognize it as an enemy 0 Answers

Trying to add a tracker to enemy AI either prefab or with reference 0 Answers

C# Enemy AI, when player is not visible i want the Enemy to chase the player for a few more seconds and then stop. 0 Answers

How get position from a prefab? 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