Question by
Clara_Krieg · Oct 08, 2021 at 04:55 AM ·
navmeshnavmeshagent
How to get AI to wander randomly around a point with NavMesh?
Hello! I am a beginner and am having trouble setting up a script to get an AI to wander around a central point. I got it to start moving, but it chooses a spot and does not update it, thus continuing to walk to the same spot. I am assuming it's because the code is in the Update method that's causing issues but I am unsure how to set it up otherwise.
Code below! I would appreciate any insight!
[SerializeField] GameObject centralWanderPoint;
NavMeshAgent agent;
[SerializeField] float movementDistance;
[SerializeField] float walkSpeed;
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
float moveX = Random.Range(1, movementDistance);
float moveZ = Random.Range(1, movementDistance);
Vector3 destination = new Vector3(moveX, 0, moveZ);
agent.speed = walkSpeed;
agent.SetDestination(centralWanderPoint.transform.position + destination);
}
Comment