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 /
  • Help Room /
avatar image
0
Question by LordShini · Nov 20, 2020 at 04:19 AM · coroutinewaitforsecondswait

Noobie question: wait a random time

Hey everyone, i'm kinda new on Unity and I'm trying to make a navmeshagent ( a spider) moving in a more or less natural way. So with a YT tuto I make a script for the spider to go to a trigger, which is randomly teleport to another position. And it's work pretty fine. But the fact that the spider never stops his movement isn't realistic at all. So i'm searching a way to make my agent to wait a random time before going to the next destination.

I've read that I should use WaitforSecond and a Coroutine, but i'm not undestand how it works. Could somebody help me to do this please?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DestinationChange : MonoBehaviour
 {
     public float xPos;
     public float yPos;
     public float zPos;
    
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "NPC")
         {
   
             xPos = Random.Range(73, 63);
             yPos = Random.Range((float)17.8, (float)17.9) ;
             zPos = Random.Range(62, 58);
 
             this.gameObject.transform.position = new Vector3(xPos, yPos, zPos);
         }
          
     }
 }
 
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 xxmariofer · Nov 20, 2020 at 09:01 AM

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.AI;
  
  public class DestinationChange : MonoBehaviour
 {
     public float xPos;
     public float yPos;
     public float zPos;
 
     private NavMeshAgent nav;
 
     private void Start()
     {
         nav = GetComponent<NavMeshAgent>();
     }
 
     private IEnumerator OnTriggerEnter(Collider other)
     {
         if (other.tag == "NPC")
         {
 
             xPos = Random.Range(73, 63);
             yPos = Random.Range((float)17.8, (float)17.9);
             zPos = Random.Range(62, 58);
 
             nav.isStopped = true;
             this.gameObject.transform.position = new Vector3(xPos, yPos, zPos);
             yield return new WaitForSeconds(Random.Range(2, 5));
 
             nav.isStopped = false;
         }
 
     }
 }

Comment
Add comment · Show 5 · 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 LordShini · Nov 20, 2020 at 07:12 PM 0
Share

Hey thanks you! Your code wasn't correct because the script was searching a navmesh on the cube (the destination), and of course only the spider has one. But I try to do this working in changing the script on the agent, and if the agent wait before his first journey until the cube, then he's not waiting at all :/

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class CharacterAgent : $$anonymous$$onoBehaviour
 {
     public GameObject CharacterDestination;
     Nav$$anonymous$$eshAgent nav;
 
     // Start is called before the first frame update
     void Start()
     {
         nav = GetComponent<Nav$$anonymous$$eshAgent>();
          
              
         
     }
 
     // Update is called once per frame
     void Update()
     {
         StartCoroutine(WaitTime());
         
 
 
        
 
     }
     IEnumerator WaitTime()
     {
         nav.isStopped = true;
         nav.SetDestination(CharacterDestination.transform.position);
         yield return new WaitForSeconds(Random.Range(2, 5));
 
         nav.isStopped = false;
     }
 }
 

avatar image xxmariofer LordShini · Nov 21, 2020 at 08:20 PM 0
Share
  private IEnumerator OnTriggerEnter(Collider other)
  {
      if (other.tag == "NPC")
      {
 
          xPos = Random.Range(73, 63);
          yPos = Random.Range((float)17.8, (float)17.9);
          zPos = Random.Range(62, 58);
 
          other.gameObject.GetComponent<Nav$$anonymous$$eshAgent >().isStopped = true;
          this.gameObject.transform.position = new Vector3(xPos, yPos, zPos);
          yield return new WaitForSeconds(Random.Range(2, 5));
 
          other.gameObject.GetComponent<Nav$$anonymous$$eshAgent >().isStopped = false;
      }
 
  }
avatar image LordShini xxmariofer · Nov 22, 2020 at 07:37 PM 0
Share

Thanks for your response and I feel we're getting close but it's still not working because "the type or namespace couldn't be found". I think the problem is due to that the agent I downloaded in the asset store is a prefab with subparts, the navmesh agent is on the main object but the collider and so the gameobject wich the script is looking for is on the child "spider" object (see the image below). alt text

How could we indicate to Unity that the nashmesh is not on the "other" object but on the main empty object the spider is in please?

explication-mouvement-aleatoire-unity-1.png (228.9 kB)
Show more comments
avatar image LordShini · Nov 21, 2020 at 05:02 PM 0
Share

Any further help would be appreciated please :)

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

223 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

StartCoroutine not listening to parameters 1 Answer

Coroutine on collision 1 Answer

Object lerping forwards a set distance every 3 seconds. 1 Answer

Unity Freezing after for loop 1 Answer

WaitForSeconds returns control to calling routine before it completes? 0 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