Hello, I’ve been trying to make a script which makes the object “wander around” and stop for 5 seconds. After that, he wanders around again. But I’m getting an error saying:
“Update() can not be a coroutine.”
This is my script so far:
var NPC : GameObject;
var Speed = 50;
var Range = 10;
var VectorRandom : Vector3;
function Update() {
NPC.transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
if((NPC.transform.position - VectorRandom).magnitude < 3) {
yield WaitForSeconds(5.0);
Start();
}
}
function Start() {
VectorRandom = Vector3(Random.Range(NPC.transform.position.x - Range, NPC.transform.position.x + Range), 1, Random.Range(NPC.transform.position.z - Range, NPC.transform.position.z + Range));
VectorRandom.y = 1;
NPC.transform.LookAt(VectorRandom);
}
I know this is because Update keeps looping it, so wait won’t work.
I’ve tried everything to fix it, but nothing works.
You a need separate function and probably a boolean or something. Sorry I don’t have Unity nor Visual Studio now, also I code in C#, but the idea would be:
bool wanderingAround;
function WanderAround {
while (true)
{
wanderingAround = true;
yield WaitForSeconds(5.0); // assuming you want it to wander for 5 seconds
wanderingAround = false;
yield WaitForSeconds(5.0);
}
Then you just do “yield StartCoroutine(“WanderAround”);” in your Start function and use the boolean “wanderingAround” in your Update function to know whether the object is supposed to be wandering or not.
By the way calling your Start function in Update is really not normal. Start is called automatically when the object is instantiated and should never be called directly. You should probably follow a couple tutorials before moving further.
Edit: Didn’t see the comment about having to check for the magnitude on every tick. The below will do the check on Update, but put the yield in the coroutine:
var NPC : GameObject;
var Speed = 50;
var Range = 10;
var VectorRandom : Vector3;
var Wandering = false : Boolean;
function Update() {
NPC.transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
if((NPC.transform.position - VectorRandom).magnitude < 3 && !Wandering) {
Wandering = true;
StartCoroutine(Wander());
}
}
IEnumerator Wander() {
yield WaitForSeconds(5.0);
VectorRandom = Vector3(Random.Range(NPC.transform.position.x - Range, NPC.transform.position.x + Range), 1, Random.Range(NPC.transform.position.z - Range, NPC.transform.position.z + Range));
VectorRandom.y = 1;
NPC.transform.LookAt(VectorRandom);
Wandering = false;
}