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 Swojak-A · Feb 07, 2021 at 04:51 PM · coroutinenavmeshagentinvoke

Problem with AI script: is there any way to add delay to a function called from Update method?

Hello,

I have a problem with my simple AI script. From what I see the problem is generated by using Invoke method to create a delay inside function the is called from the Update method.

In the script I wanted the AI agent to "wander around": travel to random destination, then when the destination is reached wait for a couple of seconds, then choose new destination and repeat the process.

Unfortunately the waiting part is not working correctly. Everything works for the first time, but in next iteration the script switches _isWandering variable also executes SetWanderPoint multiple times. Agent is frantically changing destination, but after a while everything starts to work properly again (agent reached destinations) only to repeat in next iteration.

I am quite new to C#, but I am quite proficient in Python, so I wonder why does it happen? Does Update method creates something like multiple instances of Wander method, which then overlap? Is there any advised way to have a proper delay inside a functions that are called from Update method?

Heres the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class YBotController : MonoBehaviour
 {
     private float _alertRadius = 4f;
     private NavMeshAgent _agent;
 
     [SerializeField]
     private float _maxWanderDistance;
     [SerializeField]
     private bool _isWandering;
     [SerializeField]
     private Vector3 _currentWanderingPoint;
     [SerializeField]
     private float _minWorthWanderingDistance = 1.0f;
 
     
     // Start is called before the first frame update
     void Start()
     {
         _agent = GetComponent<NavMeshAgent>();
         _isWandering = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         Wander();
     }
 
 
     private void Wander()
     {
         if (_isWandering)
         {
             _agent.SetDestination(_currentWanderingPoint);
 
             float distanceToWanderingPoint = Vector3.Distance(this.transform.position, _currentWanderingPoint);
             if(distanceToWanderingPoint < 1.0f)
             {
                 // this is the part that generates the problem
                 Invoke("ConcludeWandering", 10.0f);
                 //_isWandering = false; // this alternative is working fine
             }
         } else
         {
             _currentWanderingPoint = SetWanderPoint(this.transform.position);
             _isWandering = true;
         }
     }
 
     private Vector3 SetWanderPoint(Vector3 position)
     {
         float randomValueX = Random.Range(-_maxWanderDistance, _maxWanderDistance);
         float randomValueZ = Random.Range(-_maxWanderDistance, _maxWanderDistance);
         Vector3 newWanderingPoint = new Vector3(position.x + randomValueX, position.y, position.z + randomValueZ);
 
         float distanceToNewWanderingPoint = Vector3.Distance(position, newWanderingPoint);
 
         // if agent would only travel for a short distance, lets recursively find a new WanderPoint
         if (distanceToNewWanderingPoint < _minWorthWanderingDistance)
         {
             newWanderingPoint = SetWanderPoint(position);
         }
 
         return newWanderingPoint; 
     }
 
     private void ConcludeWandering()
     {
         _isWandering = false;
     }
 

Comment
Add comment · Show 1
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 zolteeeen · Feb 09, 2021 at 10:40 PM 1
Share

I would suggest to use IEnumerator(Coroutine) instead of doing this in Update... in IEnumerator you also have: yield return new WaitForSeconds(timeToWait) and I think that is what you are looking for.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by logicandchaos · Feb 10, 2021 at 02:26 PM

It's because you are calling Invoke() every update so you are creating many stacks to the call ConcludeWandering() this might be easier to code with a timer updated in update.

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

122 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

Related Questions

Coroutines and Invoke not working 1 Answer

How does Invoke() work? 1 Answer

Can be a problem having about 300-500 Nav mesh agents with corutines simultaneously 1 Answer

Update SetDestination only every 0.2s with coroutines ? 5 Answers

Dynamic function calling 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