How do i make the ai Enemy walk around randomly until the player is in its view area.

I’m using unity’s premade asset for a ai character, he follows me as soon as I start, what I want to do is make him walk around the level, he’s a navmesh agent, so it already does collision detection. Is there a way to make him switch between items in the map. as like an array of items. the detection thing I sphere collider with a trigger around him, but have the sphere come farther out of his chest so you can sneak up on him. if any body can help me out ill be truly thankful. (if you want to post snipets plz C#)

You surely know how to set destination of your agent using SetDestination(Vector3 target);

Then, you just have to add an array of Transform as public attribute of the class managing your agent. Place empty points randomly in your map, then, in the inspector, in the Destinations attribute of your script.

In the Start function, take a random transform among the one you have,

Then, in your Update(), test if your agent is close to its destination and chose another random transform.

I won’t give you the entire code since it’s quite simple.

Here is some links which will be usefull :

Hint : Vector3 nextDestination = Destinations[Random.Range(0, Destinations.Length - 1)].position ;

Hint : if( navmeshAgent.remainingDistance < navmeshAgent.stoppingDistance * 1.1f ) ...

Hint : I prefere checking if the agent has reached is destination by comparing its remainingDistance with its stopping distance ( > 0 ) instead of using hasPath, but it’s has you want

Don’t forget to check if the player is in sight in the Update function and stop the behaviour of “chilling out” to chase your player.

You can create an array of points in your scene with empty GameObjects, create a new flag for “EnemyTarget” or something, then on Start create an array of GameObjects with FindObjectsWithTag(“EnemyTarget”).

With this, do a check to see if the current target is the player, and if not, select a random object from the array and set destination. Then just be sure to allow the player as a target to overwrite existing blank targets when he in view.