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 Stavros-Dimou · Oct 05, 2017 at 07:16 PM · navmeshagentdestination

Destination of NavMeshAgent is not updated if the destination is a moving trasform.

Hello. It's the first project I work with NavMesh. I have the following problem: If I set the destination of a NavMeshAgent to a movable transform (for example the player), the NMA will not update its path as the target transform moves around, and it will first get to the destination it had when SetDestination() was called, and then update its destination to be the new point where the moving transform is. I don't want this behavior, instead I'd like a gameobject with a NavMeshAgent to be updating the destination (in real time) to be following something else.

How can I do that ?

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

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

Answer by FortisVenaliter · Oct 05, 2017 at 07:39 PM

By updating the NavMeshAgent's destination on a regular basis... say, every second. You don't want to do it too often, because it has to generate a new path every time.

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 Stavros-Dimou · Oct 05, 2017 at 08:34 PM 0
Share

Yup! Thank you very much! That was a part of the problem, and probably the most important. I would never guess that I had to place SetDestination() in some kind of a coroutine with WaitForSeconds() to make it work properly. I think this is some important enough piece of information to be included in the Documentation.

avatar image
1

Answer by YoucefB · Oct 05, 2017 at 08:14 PM

You can check if the target has changed its position then update the path if so, using Transform.hasChanged

Exemple

     NavMeshAgent agent;
     public Transform target;
     void Update(){
         if (target.hasChanged) {
             agent.SetDestination (target.position);
             target.hasChanged = false;
         }
     }

Note that Transform.hasChanged will be called even if the target changed its rotation or scale, so you might want to use this instead:

         NavMeshAgent agent;
     public Transform target;
     Vector3 targetOldPosition = Vector3.zero;
 
     public float checkEvery = 1; // check every one second
     float time;
     void Update(){
         time += Time.deltaTime;
         if (time >= checkEvery) {
             if (target.position != targetOldPosition) {
                 agent.SetDestination (target.position);
                 targetOldPosition = target.position;
             }
             time = 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.

Follow this Question

Answers Answers and Comments

74 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

Related Questions

A lot of navmesh agents affects ai accuracy 0 Answers

Problem with Navmesh 1 Answer

Problem accessing NavMeshAgent 2 Answers

100+ navmesh agents are not accurate 0 Answers

How to check if my player agent is at the end of the position 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