Hi guys,
I’m trying to write a simple script in order to move a NPC randomly.
I also want him to change his direction every X seconds. I found references of InvokeRepeating while doing some research and tried to use it.
My code so far is :
public class AutomaticMove2 : MonoBehaviour {
public int moveTime = 1;
public float speedTime = 5;
private const float SPEED = 5f;
private Vector3 direction;
void Update()
{
InvokeRepeating ("MoveAuto", moveTime, speedTime);
}
void MoveAuto() {
direction = (new Vector3(Random.Range(-1.0f, 1.0f), 0.0f, Random.Range(-1.0f, 1.0f))).normalized;
transform.position += direction * SPEED * Time.deltaTime;
}
}
If I do not use InvokeRepeating (and thus if I use directly the code), the code is working fine but he keeps his direction.
With InvokeRepeating, it is not working as attended : the NPC seems to change direction every .1 second or so, kind of turning on himself.
Any idea where that might come from ? Am I missing something ?
Thanks for your help!