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);
}
}
}
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;
}
}
}
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;
}
}
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;
}
}
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).
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?
Your answer
![](https://koobas.hobune.stream/wayback/20220613020132im_/https://answers.unity.com/themes/thub/images/avi.jpg)