I would like to ask if someone knows a good approach to implement an autonomous patrolling agent. By that I mean that I don’t want to give my agent a definite array of waypoints. Is there a method to create waypoints dynamically depending on the environment?
I’m thinking of these vacuum cleaner robots. I found some buzzword like “behavior based robotics” but I don’t find any explicit approaches. I think I want to equip my agent with a sensor (maybe 2 or 3 raycasts per Update) and then create a new waypoint depending on the situation.
For example: The agent should be able to decide if it turns left or right and not always choose the same direction.
Thank you guys for any help in advance and sorry for my bad english.
There are many ways to do this. But they all are overly complex or overly simplistic. The thing youre talking about is used time and time again in vehicle AIs. But it doesn’t really look good with wandering AI I have found out myself…
I don’t have much time right now, but you might want to aim for something a bit more intelligent than the robot which aimlessly washes your floors. I wouldn’t know, but it is just a common thing. Ill be back tomorrow-ish! Busy guy! Good luck.
The easiest way I do it is just generate a random point on the NavMesh and make sure it’s accessible …
This is a piece of code that I use …
function newNavMeshPoint ()
{
// get a random angle for the new navmesh point to face
angle = Random.Range(0,Mathf.PI*2);
// create a vector with length 1.0
newPosition = Vector3(Mathf.Sin(angle),0,Mathf.Cos(angle));
// scale it to the desired random length
newPosition *= Random.Range(4, 10);
// add the scaled value to the transform position
newLocalPosition = transform.position + newPosition;
// check if the new position is on the navmesh else run this function again
if(NavMesh.SamplePosition(newLocalPosition, navHit, 1, NavMesh.AllAreas))
{
finalPosition = navHit.position;
// use for testing
Instantiate(markerBall, finalPosition, Quaternion.identity);
}
else
{
newNavMeshPoint();
}
// is the finalPosition accessible, not in a building with no access, if so run this function() again
navpath = new NavMeshPath();
NavMesh.CalculatePath(transform.position, finalPosition, NavMesh.AllAreas, navpath);
if (navpath.status == NavMeshPathStatus.PathPartial || navpath.status == NavMeshPathStatus.PathInvalid)
{
newNavMeshPoint();
}
// use for testing
for (var j: int = 0; j < navpath.corners.Length - 1; j++)
Debug.DrawLine(navpath.corners[j], navpath.corners[j + 1], Color.red);
}