Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Symbie · Jul 30, 2020 at 02:36 AM · navmeshagentgizmosrandomize

NavMeshAgent keeps changing because of Vector3

So, i made a script for my NavMeshAgent, and in it, i get the size of the floor, get two random points within that, an X and a Z and set that to be the random position. i then set the navmeshagent's destination to that position. i added a gizmo to see where the destination was, and everything was working fine. then i went to make some changes, and removed the two lines of gizmo code at the bottom of my function. after this the destination keeps changing every few seconds for no reason. after it changes a dozen times or so it settles on one destinations, once it reaches it the changing starts again. why is this? This is the code for the agent:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class BotMovement : MonoBehaviour
 {
     //Time before the bot moves to its next location
     public float minWaitTime = 2f;
     public float maxWaitTime = 5f;
 
     //Gets floor object and the bounds
     public GameObject floorObject = null;
     private Bounds bounds;
 
     private NavMeshAgent nma = null;
 
     public Vector3 test;
 
     private void Start()
     {
         nma = this.GetComponent<NavMeshAgent>();
         bounds = floorObject.GetComponent<Renderer>().bounds;
     }
 
     private void Update ()
     {
         if(nma.hasPath == false || nma.remainingDistance < 1.0f)
         {
             float waitTime = Random.Range(minWaitTime, maxWaitTime);
             Invoke("SetRandomDestination", waitTime);
         }
 
     }
 
     private void SetRandomDestination()
     {
         float randomX = Random.Range(bounds.min.x, bounds.max.x);
         float randomZ = Random.Range(bounds.min.z, bounds.max.z);
         Vector3 randomPosition = new Vector3(randomX, this.transform.position.y, randomZ);
 
         nma.SetDestination(randomPosition);
 
         test = randomPosition;
       
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere(randomPosition, 50f);
     }
 }

Whenever i remove the lines of gizmo, the random position keeps changing

UPDATE!!: after mucking around for a little bit i think the problem might be with the invoke. i think it is waiting the waittime before the random destination is set, so for that whole wait time it is finding a new position until that time is up. how could i fix this? its got nothing to do with the gizmos at all

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Symbie · Jul 30, 2020 at 05:25 AM

After mucking around further, after realising what the problem was, i came to a solution on my own. i figured i might as well put it in here in case any one is interested. because the invoke was messing stuff up, and making the destination reset itself until the time was up, i created a CoRoutine and at the beginning i set the speed of the agent to 0. then it got the destination, waited for the random time then set the speed back to normal, this way the destination is set and the agent still waits! using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices.WindowsRuntime; using UnityEngine; using UnityEngine.AI;

 public class BotMovement : MonoBehaviour
 {
     //Time before the bot moves to its next location
     public float minWaitTime = 2f;
     public float maxWaitTime = 5f;
 
     //Gets floor object and the bounds
     public GameObject floorObject = null;
     private Bounds bounds;
 
     private NavMeshAgent nma = null;
 
     public Vector3 randomPosition;
 
     private void Start()
     {
         nma = this.GetComponent<NavMeshAgent>();
         bounds = floorObject.GetComponent<Renderer>().bounds;
     }
 
     private void Update ()
     {
         if(nma.hasPath == false || nma.remainingDistance < 1.0f)
         {
             StartCoroutine(SetRandomDestination());
         }
 
     }
 
     private IEnumerator SetRandomDestination()
     {
         float waitTime = Random.Range(minWaitTime, maxWaitTime);
         nma.speed = 0f;
 
         float randomX = Random.Range(bounds.min.x, bounds.max.x);
         float randomZ = Random.Range(bounds.min.z, bounds.max.z);
         randomPosition = new Vector3 (randomX, this.transform.position.y, randomZ);
 
         nma.SetDestination(randomPosition);
 
         yield return new WaitForSeconds(waitTime);
 
         nma.speed = 5f;
     }
 
     public void OnDrawGizmos()
     {
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere(randomPosition, 0.5f);
     }
 }
 
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
0

Answer by SimonsCat · Aug 25, 2021 at 01:10 PM

Every frame that the update happens during the "waitTime" the Invoke is called. For example if the wait time is 60s and the Update is running at 60FPS the Invoke gets called for 60 times until the first invoke call starts with setting the destination...

And even the SetDestination method sometimes needs more frames to calculate the path. I guess that hasPath returns false until the entire path is not calculated. You should probably use the NavMeshAgent.pathPending property too.

 private void Update ()
      {
          if(nma.hasPath == false || nma.remainingDistance < 1.0f)
          {
              float waitTime = Random.Range(minWaitTime, maxWaitTime);
              Invoke("SetRandomDestination", waitTime);
          }
      }

I would guess that here is the main problem: if(nma.hasPath == false || nma.remainingDistance < 1.0f)

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

134 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

Related Questions

array with a list of materials 3 Answers

Random objects in coordinates specific 1 Answer

Randomly instantiate objects of an array, but each only once. The Object you want to instantiate is null. 1 Answer

Draw Gizmos and Handles in the scene from an EditorWindow 1 Answer

Draw Gizmos without Depth-test 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