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 Rizzutp · Mar 17, 2018 at 03:48 PM · navmeshnavmeshagentpathfinding

How to prevent a NavMeshAgent to constantly spin along its y axis?

I did this C# script and attached to my NPC, which is supposed to continuously patrol a maze. The agent seems to move correctly from one point to another, but sometimes, while changing its direction, it starts to spin along its y axis without stopping! I would like to prevent this behavior butI don't know what it's going wrong. This is the code of my script:

 using UnityEngine;
 using UnityEngine.AI;
 using System.Collections;
 
 
 public class MyChase : MonoBehaviour {
 
     public Transform[] points;
     private Transform play;
     private int destPoint = 0;
     private NavMeshAgent agent;
     private GameObject pointsHolder;
     private int numOfPoints;
     Animator anim;
 
     void Start () {
         agent = GetComponent<NavMeshAgent>();
         play = GameObject.FindWithTag("Player").transform;
         anim = GetComponent<Animator> ();
 
         agent.autoBraking = false;
 
         pointsHolder = GameObject.FindGameObjectWithTag("PatrolPath");
         numOfPoints = pointsHolder.transform.childCount;
         points = new Transform[numOfPoints];
 
         for (int t = 0; t < numOfPoints; t++) {
             points [t] = pointsHolder.transform.GetChild (t).transform;
         }
 
 
         GotoNextPoint();
     }
 
 
     void GotoNextPoint() {
 
         if (points.Length == 0)
             return;
 
         agent.destination = points[destPoint].position;
 
         destPoint = (destPoint + 1) % points.Length;
     }
 
 
     void Update () {
 
         if (!agent.pathPending && agent.remainingDistance < 0.5f)
             GotoNextPoint();
     }
 }

P.s.: I have to add the animation after solving this problem, if you're asking to yourself why I've declared an animator object.

Comment
Add comment · Show 4
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 RobAnthem · Mar 17, 2018 at 04:22 PM 1
Share

You need to use agent.SetDestination(destination) because if you just set the destination variable, it won't recalculate his path. Which could easily lead to some problems, especially if there are moving objects in the scene.

Also, I found that this works better than checking the agents distance.

 void Update()
 {
     if (Vector3.Distance(transform.position, agent.destination) < 0.1f)
     {
         GotoNextPoint();
     }
 }
avatar image Rizzutp RobAnthem · Mar 17, 2018 at 04:31 PM 0
Share

I applied what you suggested, but unfortunately the NPC behavior remains the same

avatar image RobAnthem Rizzutp · Mar 17, 2018 at 04:42 PM 1
Share

Well it could be your code at setting the point. You should linearly walk the nodes ins$$anonymous$$d of what your doing. Like this;

 private int currentDestination;
 public Transform[] destinations;
 void SetNextPoint()
 {
     if (currentDestination +1 < destinations.Length)
     {
         currentDestination+=1;
     }
     else
     {
         currentDestination = 0;
     }
     agent.SetDestination(currentDestination);
 }

Alternatively, if you want him to walk back the exact path he came...

     private int currentDestination;
     public Transform[] destinations;
     private bool direction;
     void SetNextPoint()
     {
         if (direction)
         {
             if (currentDestination +1 < destinations.Length)
             {
                 currentDestination+=1;
             }
             else
             {
                 direction = false;
                 currentDestination -=1;
             }
         }
         else
         {
             if (currentDestination -1 > -1)
             {
                 currentDestination-=1;
             }
             else
             {
                 direction = true;
                 currentDestination +=1;
             }
         }
         agent.SetDestination(currentDestination);
     }
Show more comments

1 Reply

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

Answer by suIly · Mar 17, 2018 at 11:48 PM

Add a rigid body to the object and add a constraint to the y rotation.

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 Rizzutp · Mar 20, 2018 at 08:34 PM 0
Share

It's a bit better, thanks! However, I discovered that my issue derived from my Nav$$anonymous$$eshSurface.

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

82 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

Related Questions

Navmesh with destructible obstacles 1 Answer

How to sample a point on a navMesh Agent path 0 Answers

Connecting two navmeshes without "Speedboost" 0 Answers

NavMeshAgent Not Accounting for Objects 0 Answers

How to display the trajectory of a nav mesh agent in runtime ? 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