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 intelligent_chicken · Apr 26, 2020 at 05:34 AM · aibeginnerarraysenemy aipatrol

Why won't my AI change waypoints for patroling?

Hello There! I made a enemy AI script. In that AI script I have a patrol state. That patrol state does not work properly. The waypoints for the AI is an array of gameobjects. The AI will go to point 0, then it will start rotating crazily and it won't move to the next waypoint. Here's the Patrol part of the code:

  IEnumerator Patrol()
     {
         patrol = true;
         canSeePlayer = attacking = isSearching = false;
         waitTime = startWaitTime;
         amount = 0;
         agent = GetComponent<NavMeshAgent>();
         moveSpotsStop = 1;
 
         agent.destination = moveSpots[0].position;
         agent.speed = patrolSpeed;
         Vector3 moveSpotsPosition = new Vector3(moveSpots[amount].position.x,
                                          transform.position.y,
                                          moveSpots[amount].position.z);
         transform.LookAt(agent.destination);
 
         while (patrol == true)
         {
 
             if (Vector3.Distance(transform.position, agent.destination) <= moveSpotsStop)
             {
                 arrivedAtPoint = true;
                 amount += 1;
 
                 agent.destination = moveSpots[amount].position;
                 agent.speed = patrolSpeed;
 
                 if (amount >= moveSpots.Length - 1)
                 {
                     amount = 0;
                 }
                 
 
                 
                 transform.LookAt(agent.destination);
 
             }
             else
             {
                 arrivedAtPoint = false;
             }
 
 
             if (canSeePlayer)
             {
                 patrol = false;
                 stateNumber = 3;
                 break;
                 
             }
 
             yield return null;
         }
         yield return new WaitForSeconds(0);
     }

The bool arrived at point is set to true when the AI arrives at the first point, but it won't move to point 2.

Can anyone please help me? I've been at this problem for over 2 days with 15 hours. Help will be appriciated. Thank you.

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
0

Answer by nickk2002 · May 02, 2020 at 06:15 AM

First of all transform.LookAt(agent.destination); is not a good idea to use when moving with navmeshagent. The agent already takes care of rotation by himself and what I think it happens is that two components try to modify rotation at the same time. That is maybe why you have a awkward rotation.

Secondly, the check of the amount being bigger than array (line 27) size should be put before you actually access the array index. It will be out of bounds if you keep it like this. And get a nice exception`"System.IndexOutOfRangeException: Index was outside the bounds of the array"`

Apart from that every thing should be ok. As advice you should not call GetComponent<> in a function if the function is called more times, just cache it in the Start() and Awake(), this also makes the code cleaner.

As for the implementation unity coroutines are cool, I have to admit. I usually prefer another way to handle states using a enum. https://pastebin.com/L2J9KtrJ here is a snippet of my current work in progress for an ai. Also a pro tip about how to set the patrol points in a easy way to preview in scene view. https://ibb.co/FB6sFQK unity gizmos is pretty cool right? @intelligent_chicken

Good luck with you ai!. Cheers :)

Best Regards,

Nick

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

216 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 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 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

Enemy AI Patrol Points Array Index Out of Range? 2 Answers

free roaming enemy ai 1 Answer

Enemy nearest waypoint detector using LINQ, I need help updating the patrol array index using info from LINQ 1 Answer

Raycasting AI 0 Answers

Why is everything being set the same? Please Help! 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