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 kalledk21 · Jun 12, 2016 at 03:33 PM · pathing

Why is my navmesh enemy pathing not working?

So I have created this little script to take an array of transforms and make the enemy walk from point to point in circles.

It successfully adds the transforms to the array without any errors, however my character doesn't move at all, any ideas anyone?


 using UnityEngine;
 using System.Collections;
 
 public class EnemyPathing : MonoBehaviour {
 
     [SerializeField]
     Animator anim;
 
     [SerializeField]
     NavMeshAgent nMA;
 
     [SerializeField]
     Transform[] pathPoints;
 
     GameObject path;
 
     int nr;
 
     void Awake()
     {
         anim = GetComponent<Animator>();
         nMA = GetComponent<NavMeshAgent>();
 
         path = GameObject.Find(transform.name + "Path");
 
         pathPoints = new Transform[path.transform.childCount];
         for (int i = 0; i < path.transform.childCount; i++)
         {
             pathPoints[i] = path.transform.GetChild(i);
         }
 
         nMA.Resume();
     }
 
     void Update ()
     {
         if (nMA.remainingDistance > nMA.stoppingDistance)
         {
             nMA.destination = pathPoints[nr].position;
             
         }
         else if (nMA.remainingDistance <= nMA.stoppingDistance)
         {
             if(nr < pathPoints.Length)
             {
                 nr++;
             }
             else
             {
                 nr = 0;
             }
             nMA.Resume();
         }
     }
 }


Thank you in advance

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

1 Reply

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

Answer by Nucky9 · Jun 12, 2016 at 10:06 PM

I managed to find a few problems. First, you never set the initial destination, so your agent never has a distance to test against your if statements. Also, your 'nr < pathPoints.Length' needs to be 'nr < (pathPoints.Length - 1)', since the index starts at 0, not 1.

Finally, I don't understand the point of the loop you are using to build your path (although I am pretty new at Unity, so I might be missing something). Right now, you can drag the waypoints directly into the navagent's inspector to build the path list. Do you want to build the list automatically instead? If so, what are you trying to build the list from, all objects that have 'path' in their name?

Anyway, assuming you populate the navagent's path list manually, the following code works to get the navagent looping through all the path points:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyPathing : MonoBehaviour
 {
 
     [SerializeField]
     NavMeshAgent nMA;
 
     [SerializeField]
     Transform[] pathPoints;
 
     // GameObject path;
 
     int nr = 0;
 
     void Awake()
     {
         nMA = GetComponent<NavMeshAgent>();
 
         //path = GameObject.Find(transform.name + "Path");
 
         //pathPoints = new Transform[path.transform.childCount];
         //for (int i = 0; i < path.transform.childCount; i++)
         //{
         //    pathPoints[i] = path.transform.GetChild(i);
         //}
         nMA.destination = pathPoints[nr].transform.position;
     }
 
     void Update()
     {
         if (nMA.remainingDistance <= nMA.stoppingDistance)
         {
             if (nr < (pathPoints.Length - 1))
             {
                 nr++;
                 nMA.destination = pathPoints[nr].transform.position;
 
             }
             else
             {
                 nr = 0;
                 nMA.destination = pathPoints[nr].transform.position;
             }
         }
     }
 }

Hope that helps!

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 kalledk21 · Jun 13, 2016 at 03:59 PM 0
Share

Thanks a lot!

As always the problem seems to be that I'm apparently blind... Anyway as for the initial loop, it was a simple setup to get ready to automate the process if i decided to change/add paths later.

To anyone who might need it Here is the complete script with a few comments for understanding

 using UnityEngine;
 using System.Collections;
 
 public class EnemyPathing : $$anonymous$$onoBehaviour {
 
     [SerializeField]
     Animator anim;
 
     [SerializeField]
     Nav$$anonymous$$eshAgent n$$anonymous$$A;
 
     [SerializeField]
     Transform[] pathPoints;
 
     GameObject path;
 
     int nr;
 
     void Awake()
     {
         anim = GetComponent<Animator>();
         n$$anonymous$$A = GetComponent<Nav$$anonymous$$eshAgent>();
 
         //Find the holder of the path, in my case
         //the script is on a prefab called Enemy,
         //thus the path-holder should be named "EnemyPath"
         //Just to keep myself systematic
         path = GameObject.Find(transform.name + "Path");
 
         //I loop through each child in the path-holder
         //each child is an empty gameobject
         //This is to keep levelcreation fast and easy to understand
         pathPoints = new Transform[path.transform.childCount];
         for (int i = 0; i < path.transform.childCount; i++)
         {
             pathPoints[i] = path.transform.GetChild(i);
         }
 
         //First destination is set to the first child
         //This is since the int will default to 0
         n$$anonymous$$A.destination = pathPoints[nr].position;
 
         n$$anonymous$$A.Resume();
     }
 
     void Update ()
     {
         //This were we move
         //As long as we haven't reached our destination yet
         //we'll move towards it
         //Once we reach it, the next pathpoint is chosen
         //Or we choose the first point again to circle
         if (n$$anonymous$$A.remainingDistance <= n$$anonymous$$A.stoppingDistance)
         {
             if (nr < (pathPoints.Length - 1))
             {
                 nr++;
                 n$$anonymous$$A.destination = pathPoints[nr].transform.position;
 
             }
             else
             {
                 nr = 0;
                 n$$anonymous$$A.destination = pathPoints[nr].transform.position;
             }
         }
     }
 }
 

By any means feel free to use it

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Astar Pathfinding - do not use last visited node 1 Answer

newb project setup questions 1 Answer

Can anyone help with enemy pathing within a 2D shooter? 0 Answers

Curving a Path Around an Obstacle 0 Answers

How do I shoot a bullet that follows a sine wave? 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