So I’ve been doing a little research about creating a wandering AI and was hoping someone knew a little bit more than me about setting one up. Here’s the code for spawning an object in a random part of a circle radius
The problem is what if I want this circle to be applied to the area around an empty gameobject? Since I want enemies to walk around this set area instead. Also for the enemies movement would I just store a variable for that random location and then when the enemy reaches the location wait a random time before moving to a new location inside of the circle area.
This is what got me started on basic AI and a good place to go from IMO
This kind of helps me out but I still need help explaining how to setup the random circle radius from an empty gameobject location so my enemy can calculate where to walk towards.
Well at the moment the get target function is essentially getting a random point in a square thats 300 wide/long and centered at (150,150). So for a circle distance from a certain object just changed that random.range to your circle function and maybe define your 'empty gameobject' as a public variable in the AI script so the bot always knows the area it needs to be wandering in.
I’ll give you a simple solution I learned from an expert AI programmer. What you’re doing is creating a virtual circle in front of your AI with a given radius and ahead of the character by a certain distance. Frame by frame you pick a random point along the curve of the circle and make the NavMeshAgent steer toward it. A factor for “jitter” determines how much randomness and direction changing takes place. By adjusting the values of the circular radius, the distance ahead and the jitter you can figure out the right type of wandering behavior for your NPC AI character by simply changing the values in the editor and watching how they move around. Experiment with the values until it looks right.
[SerializeField] float circRadius = 10f; // Radius of target circle
[SerializeField] float circDistance = 10f; // Distance of circle in front of the AI agent
[SerializeField] float wanderJitter = 1f; // Amount of random jitter in steering
Vector3 aiWanderGoal = Vector3.Zero;
void WanderAround() {
/* We project a (mathematical) circle in front of the AI agent with a size of circRadius
* and a distance in front of circDistance. We then pick a random place along the curve
* of the circle to move toward, randomized by the factor of wanderJitter ...
*/
aiWanderGoal += new Vector3( Random.Range( -1.0f, 1.0f ) * wanderJitter, 0f,
Random.Range( -1.0f, 1.0f ) * wanderJitter );
aiWanderGoal.Normalize();
aiWanderGoal *= circRadius;
var locTarget = aiWanderGoal + new Vector3( 0, 0, circDistance );
var worldCoord = gameObject.transform.InverseTransformVector( locTarget );
MoveTo( worldCoord );
}
void MoveTo( Vector3 location ) {
// This is your own code that tells the NavMeshAgent where to go ...
// But it could be as simple as this:
navMeshAgent.SetDestination( location );
}
Another approach is to spawn a randomized Vector3 every so often and set it as the NavMeshAgent destination, but you have to be careful your target won’t be invalid (i.e., not on the terrain or navigable surface of the world) and clamp the values down to a certain range. This has worked decently for me in some larger open world terrains and I used terrain height sampling to find the correct Y value of the target point. Set a reasonable stopping distance for the agent in your world that works well: they don’t need to get directly on top of their goal/destination unless that’s important (like you need them standing on an exact spot for a cut scene or something.
Good luck!
Note: Update would call WanderAround(); while you want the NPC to randomly wander around. This could even be expanded in a game where you want AI to explore the map, like in an RTS with "fog of war" by weighting the target point toward unexplored areas. The same thing could be done by weighting the point toward the player if you want the AI to appear to be randomly searching for the player after hearing/detecting them. Really good AI can be built up by combining lots of simple and cleanly written behaviors!
This kind of helps me out but I still need help explaining how to setup the random circle radius from an empty gameobject location so my enemy can calculate where to walk towards.
– jessee03Well at the moment the get target function is essentially getting a random point in a square thats 300 wide/long and centered at (150,150). So for a circle distance from a certain object just changed that random.range to your circle function and maybe define your 'empty gameobject' as a public variable in the AI script so the bot always knows the area it needs to be wandering in.
– xt-xylophone