Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Polinator · Mar 20, 2013 at 05:11 PM · navmeshagentpathfindingnpc

How do you set up navmesh agents to go to multiple destinations?

I looked at some tutorials and the documentation on how NavMeshAgents work, and currently have NPC's going to a target in my scene. I can't seem to find where the documentation tells me how to setup multiple destinations for my NPC though. Here is my code for the NavMesh with one target:

 public var target : Transform;
 
 function Start () {
     GetComponent(NavMeshAgent).destination = target.position;
 }

Now here is what I thought up in order to do multiple positions:

 public var currentTarget : Transform;
 public var targets : Transform[];
 public var navigation : NavMeshAgent;
 public var i : int = 0;
 
 
 function Start () {
     
     navigation.destination = targets[i].position;
 
 }
 
 function Update () {
     var dist = Vector3.Distance(targets[i].position,transform.position);
     currentTarget = targets[i];
     
     
     
     //if npc reaches its destination...
     if (dist < 5){
         //go to next target by setting it as the new destination
         navigation.destination = navigation.nextPosition;
         if (i < targets.Length){
             //change next target
             i++;
             navigation.nextPosition = targets[i].position;
         }
     }
     
 
 }

At the moment the NPC is warping all the way down to the last transform in targets[], so I think it's because "i" is getting incremented too fast? How should I fix this?

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
4
Best Answer

Answer by Polinator · Mar 20, 2013 at 05:49 PM

I figured it out a fix for myself, but it doesn't use nextPosition, so if anyone would like to chime in and explain how I would exactly use that, it'd be great.

Here's my fix, everything works fine:

 public var currentTarget : Transform;
 public var targets : Transform[];
 public var navigation : NavMeshAgent;

 private var i : int = 0;
 
 
 function Start () {
     
     navigation.destination = targets[i].position;
 
 }
 
 function Update () {
     var dist = Vector3.Distance(targets[i].position,transform.position);
     currentTarget = targets[i];
     
     
     
     //if npc reaches its destination (or gets close)...
     if (dist < 5){        
         if (i < targets.Length - 1){ //negate targets[0], since it's already set in destination.
             i++; //change next target
             navigation.destination = targets[i].position; //go to next target by setting it as the new destination
         }
     }
     
 
 }
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 ravindra123 · Jun 13, 2013 at 05:26 PM 1
Share

Yeh,,,Thanks Buddy, Its Really useful for me..

avatar image diggerjohn · Jan 21, 2014 at 03:54 PM 0
Share

Sweet, thanks for posting this.

avatar image srmojuze · May 18, 2015 at 03:23 PM 0
Share

Thanks. $$anonymous$$odified to cycle through targets, to "loop" the NPC thingy visiting all different points. Could potentially be modified to find random targets.

avatar image
2

Answer by srmojuze · May 18, 2015 at 03:22 PM

Hi, to cycle between destinations:

 #pragma strict
 
 
 private var targets : GameObject[];
 private var navigation : NavMeshAgent;
 private var i : int = 0;
      
      
 function Start () 
       {
         navigation = GetComponent(NavMeshAgent);
          targets = GameObject.FindGameObjectsWithTag("NavTarget");
         
          //set first target
         navigation.destination = targets[i].transform.position;     
          }
      
 function Update () 
          {
          var dist = Vector3.Distance(targets[i].transform.position,transform.position);
          //currentTarget = targets[i].transform;
          //if npc reaches its destination (or gets close)...
          if (dist < 2)
          {                
                 i++; //change next target      
                 if (i < targets.Length )
                  { 
                 navigation.destination = targets[i].transform.position; //go to next target by setting it as the new destination
                  }
               
              //check if at end of cycle, then reset to beginning of cycle
          if (i == targets.Length )
                  {
                  Debug.Log("NAVIGATION FINISHED. RESET.");
                  i = 0;
                  navigation.destination = targets[i].transform.position;
                  }
         }
 }
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
2

Answer by demented_hedgehog · Aug 10, 2015 at 01:33 AM

Link to the docs... (I imagine that these docs are newer than the question). http://docs.unity3d.com/Manual/nav-AgentPatrol.html

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

15 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

Related Questions

How to change a NavMeshAgent angular rotation speed, 0 Answers

My NPC is not switching from running away state to random movement state. 1 Answer

CalculatePath often returns wrong results 0 Answers

Navmesh with destructible obstacles 1 Answer

How to sample a point on a navMesh Agent path 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