Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by techspy · Apr 22, 2016 at 07:00 AM · mecanimnavmesh

Stop a Navmesh agent mid path

i have a third person hack 'n slash i'm working on and no, it is not a button masher. The combat needs skill to execute attacks plus no auto targeting. Anyhoo, I've hot the player side of things set up and now comes the AI. I'm using Navmesh and mecanim to move the agents. The agent has a rigidbody with a custom third person controler attached. I can get him to patrol nicely. Then the player shows up and he has to stop and come towards the player. However my agent is determined to finish his patrol route.

 void Start (){
 navmeshAgent.updateRotation = false;
 navmeshAgent.updatePosition = true;
 target = GameObject.FindGameObjectWithTag("waypoint2").transform;
 //find game object will change to specific transforms later
 }
 
 //then later in update:
 navmeshAgent.SetDestination(target.position);
 
         if (navmeshAgent.remainingDistance > navmeshAgent.stoppingDistance)
             enemyChar.move(navmeshAgent.desiredVelocity);
         else
             enemyChar.move(Vector3.zero);
             
 //much later on I pass the Vector3 values to mecanim and animate the mesh.
 //this is in another class though

Now go ahead and try to enemyChar.move(Vector3.zero); He wont stop. navmeshAgent.ResetPath()? No thanks. Set him a new destination? He likes his old one. I did manage to het him to stop by miving the waypoint but that's not the way to do it I think. Help.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by M-Hanssen · Apr 22, 2016 at 10:10 AM

 navmeshAgent.Stop();
 navmeshAgent.ResetPath();

Should do the trick

Comment
Add comment · Show 2 · 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 techspy · Apr 22, 2016 at 12:15 PM 1
Share

Sir/ma'am you under estimate the deter$$anonymous$$ation of my enemy

avatar image Tabu · Oct 22, 2017 at 06:55 AM 0
Share

This did the trick for me - but without the navmeshAgent.Stop(); since this have been deprecated. Thanks

avatar image
1

Answer by carlqwe · Apr 22, 2016 at 09:59 AM

If you want to stop the moving object when its for instance half way to its destination, then u can set the destination to the objects current position.

 MyNavMeshAgent.destination = gameObject.localPosition;

Or even easier set the target to the gameObjects local position

 target = gameObject.transform.localPosition;

Doesn't work? Check out this unity documentation

Comment
Add comment · Show 1 · 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 techspy · Apr 22, 2016 at 12:13 PM 0
Share

I tried transform.gameObject.transform.position. The enemy (yes he's become my enemy) just ignores me. But if I move the transform of the waypoint he responds. It's like i can't modify anything related to the NavmeshAgent at all. Setting the move Vector to zero just makes him jerk a bit then stop. The code is modified from the unity's examples

avatar image
0

Answer by noGrip · Aug 30, 2021 at 08:28 AM

It has been some years since the last reply. But this works for me now:

 nav.velocity = Vector3.zero;
 nav.isStopped = true;
Comment
Add comment · Show 3 · 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 NordicMothCEO · Aug 31, 2021 at 12:50 PM 0
Share

It keeps complaining for me, whenever enemy gets in range and keeps on moving on, even though it should stop.

Here's my error: "Stop" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.StackTraceUtility:ExtractStackTrace ()

Can you help with this, please?

avatar image noGrip NordicMothCEO · Aug 31, 2021 at 01:38 PM 0
Share

Don't know if I can help much, without seeing a code snippet. But the error description seems to indicate that your agent is not on the NavMesh.

I would check NavMesh.SamplePosition: [link text][1] [1]: https://docs.unity3d.com/ScriptReference/AI.NavMesh.SamplePosition.html

avatar image NordicMothCEO noGrip · Aug 31, 2021 at 02:55 PM 0
Share

I decided not to make it stop, but to try and approach the enemy if it gets in range. It works a lot better, than stopping would have, so I'm happy with it.

The lines that make it work basically are as follows:

     public void AttackEnemy()
     {
         if (Target == null)
         {
             Attack(false);
             Move();
         }
         else
         {
             float maxDistance = Vector3.Distance(transform.position, Target.transform.position);
             stance.LookAt(Target.transform);
 
             if (maxDistance < range)
             {
                 Attack(true);
                 Approach();
             }
             else
             {
                 Attack(false);
                 Move();
             }
         }
     }
 
     public void Attack(bool isActive)
     {
         var emissionModule = projectileParticles.emission;
         emissionModule.enabled = isActive;
     }
 
     public void Move()
     {
         NavMeshAgent agent = GetComponent<NavMeshAgent>();
         agent.SetDestination(goal.transform.position);
     }
 
     public void Approach()
     {
         NavMeshAgent agent = GetComponent<NavMeshAgent>();
         agent.SetDestination(Target.transform.position);
     }

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

9 People are following this question.

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

Related Questions

Enemy follow player on navigation mesh 0 Answers

Getting turning direction for character without using Input 1 Answer

Rotate animation at waypoint 1 Answer

NavMeshAgent: Keep Enemy x Distance from Player? 1 Answer

Teddy Bear Mecanim NavMesh (my animations don't work) 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