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 Dothackking · Jun 27, 2014 at 10:07 PM · c#ainavmeshagent

Why isn't my enemy pausing in between waypoints? C#

My enemy is using a completely custom AI made using nothing but unity's default tools. It uses a NavMesh and NavMeshAgents.

I'm having problems where I want my enemy to stop for 4 seconds after each waypoint is reached, but they're not stopping.

Edit: It also seems that as time goes on the AI starts doing more and more new paths at one time. It starts out working perfect, after 30 seconds it starts doing two path calculations rapidly, a minute, and we're up to 3.

Here's my code:

             if (nav.remainingDistance < 0.1)
             {
                 nav.Stop();
                 nav.ResetPath();
                 if(hasMoved && !newDestCalled)
                 {
                     Debug.Log ("Trying to call New Destination");
                     newDestCalled = true;
                     StartCoroutine(NewDestination ());
 
                 }
                 else
                 {
                     waypointRandom = Random.Range(0, waypoints.Length);
                     Debug.Log (waypointRandom);
                     nav.SetDestination(waypoints[waypointRandom].position);
                     hasMoved = true;
                 }
 


 
     IEnumerator NewDestination()
     {
         yield return new WaitForSeconds(4);
         waypointRandom = Random.Range(0, waypoints.Length);
         Debug.Log (waypointRandom);
         nav.SetDestination(waypoints[waypointRandom].position);
         newDestCalled = false;
 
     }
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 tanoshimi · Jun 27, 2014 at 10:16 PM

I'm assuming that your if (nav.remainingDistance < 0.1) bit is called in an Update() loop? If so, there's a flaw in your logic.

It's unclear what the ifMoved variable does, but it actually doesn't matter. Let's assume that ifMoved is true to start with, and that newDestCalled is false, in which case the first if block gets executed and calls your NewDestination coroutine (which is where you're assuming a 4 second pause will be introduced).

But, on the very next frame, newDestCalled is now true (since it was set in line 8), which means that the else block now gets executed and immediately sets a new nav destination, without waiting... so there's never a time when the pause introduced by the coroutine would take be noticeable.

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 Dothackking · Jun 27, 2014 at 10:59 PM 0
Share

It should be the other way around. newDestCalled is BEFORE the coroutine, so shouldn't it be executed first?

Yes it's being called in update. I didn't see any way to clear the destination when it was reached, so this was my logic for doing that. has$$anonymous$$oved is essentially if they've already done the initial movement, I don't know why it's in there, looking at it now....haha.

Guess it's just a relic of the way I had it working to get it to this point.

I think I may see what you're talking about, let me try to fix it real quick.

Okay your first part confused me, that was indeed exactly right! Thank you. To sum it up:

$$anonymous$$y 2nd part of the if statement was a relic and unneeded, and was causing the problems.

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

21 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

Related Questions

How do I limit the path of a NavMeshAgent? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NavMeshAgent Finding Alternative Route 1 Answer

Enemy Spawn Script Spawning Infinite Enemies [Help] 2 Answers

How do I get my emeny AI to detect if he sees a player, or one of the good AI? (C#) 3 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