• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by fxk · Nov 26, 2013 at 08:04 PM · ainavmeshnavmeshagent

Navmeshagent runs in circles

I'm working on a game in which an NPC chases the player character. The NPC is a Navmeshagent and I've modified the Agent.cs script that comes with one of the Unity projects. The NPC behaves properly most of the time, but if the Player character runs into the NPC, the NPC can start to run around in circles. Eventually, after spinning around for a while, the NPC will seem to find its bearings and again pursue the character, but this can take 5-10 seconds.

I've tried resetting the path and destination every 1/2 second (on the idea that maybe the path was getting screwed up), but that only seemed to make things worse. And, when I did that, I did check to see that pathPending was false so the NPC at least thinks it knows where to go.

Here's the bulk of code that I think is relevant (from the Agent.cs script); SetupAgentLocomotion gets called in Update.

 protected void SetupAgentLocomotion()
 {
     //Debug.Log ("Calling SetupAgentLocomotion.");
     bool attacking = animator.GetBool("Attacking");
     
     
     if (AgentDone() || attacking)
     {
           
         Vector3 diff = gameObject.transform.position - target.transform.position;

         npc.transform.LookAt(target.transform.position);

                  locomotion.Do(0, 0);

                  if (!attacking) { StartCoroutine(Attack ()); }
     }
     else
     {

         if (float.IsNaN(agent.remainingDistance) || float.IsInfinity(agent.remainingDistance)) {
             agent.ResetPath();
             agent.SetDestination(target.transform.position);
         }
         
         float speed  = agent.desiredVelocity.magnitude;

         Vector3 velocity = Quaternion.Inverse(transform.rotation) * agent.desiredVelocity;

         float angle = Mathf.Atan2(velocity.x, velocity.z) * 180.0f / 3.14159f;

                     locomotion.Do(speed, angle);

     }
     
 }
 
 IEnumerator Attack() {
     timeOfLastAttack=Time.time;
     animator.SetBool("Attacking",true);

     if (TargetInRange()) {
         // this assumes using Target empty GO
         target.parent.SendMessage("Damage",DAMAGE_INFLICTED);
     }

             yield return new WaitForSeconds(1.0f * ATTACK_DELAY);
     animator.SetBool("Attacking",false);
     agent.SetDestination(target.transform.position);

}

I've been struggling with this for a while, so any insight anyone has would be extremely welcome. Sorry if the code formatting is a bit weird -- after several edits it still isn't quite right.

Thanks in advance.

Comment
Add comment · Show 1
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 PNUMIA-Rob · Jan 05, 2017 at 07:04 AM 0
Share

Having the same problem, would be awesome to find a solution.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by McGravity · Jan 05, 2017 at 08:42 AM

Your NavMeshAgent has probably some angular speed. Thus it is not possible to walk around in arbitrary small turning circles. When approaching the target in a weird angle it will try to turn (while still walking) with its maximum angular speed, which is not enough to make a steeper turn.

So either you increase the angular speed which might look unnatural or you implement something like "FaceBeforeMoveAngle" where you first check whether the angle is smaller than a certain value before the agent starts moving. Otherwise it will only turn.

For my agent it looks roughly like this:

 Vector3 velocity = Quaternion.Inverse(agent.transform.rotation)
     * agent.Agent.desiredVelocity;
 float angle = Mathf.Atan2(velocity.x, velocity.z) * 180.0f / Mathf.PI;
 
 float speed = agent.Agent.desiredVelocity.magnitude;
 
 if (angle > agent.FaceBeforeMoveAngle) {
     agent.Do(0, angle);
 } else {
     agent.Do(speed, angle);
 }

You would compute the FaceBeforeMoveAngle everytime you change the agents destination. I'm using that extra agent variable because I'm working with an Entity-System architecture. So just remove it and work directly on the NavMeshAgent if you want to.

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
avatar image
0

Answer by lufactus · Sep 22, 2021 at 10:51 PM

I solved it setting stoppingDistance anything higher than 0.

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

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

19 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

Related Questions

Need help detecting barriers for my game 0 Answers

NavMesh flee. Ai flee from player. 4 Answers

Connecting two navmeshes without "Speedboost" 0 Answers

Runtime Navmesh not working on mobile build 0 Answers

Rotating the a NavMeshAgent when going up- or downhill? 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