Search Unity

Creating an autonomous patrolling agent

Discussion in 'Navigation' started by Ipsider, Aug 18, 2015.

  1. Ipsider

    Ipsider

    Joined:
    Jul 7, 2015
    Posts:
    3
    Hey there,

    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.

    Regards,
    Philipp
     
  2. Lahzar

    Lahzar

    Joined:
    May 6, 2013
    Posts:
    87
    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! :p Good luck.
     
  3. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    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 ..

    Code (JavaScript):
    1. function newNavMeshPoint ()
    2. {
    3. // get a random angle for the new navmesh point to face
    4.     angle = Random.Range(0,Mathf.PI*2);
    5. // create a vector with length 1.0
    6.     newPosition = Vector3(Mathf.Sin(angle),0,Mathf.Cos(angle));
    7. // scale it to the desired random length
    8.     newPosition *= Random.Range(4, 10);
    9. // add the scaled value to the transform position
    10.     newLocalPosition = transform.position + newPosition;
    11. // check if the new position is on the navmesh else run this function again
    12.     if(NavMesh.SamplePosition(newLocalPosition, navHit, 1, NavMesh.AllAreas))
    13.     {
    14.         finalPosition = navHit.position;
    15. // use for testing
    16.         Instantiate(markerBall, finalPosition, Quaternion.identity);
    17.     }
    18.     else
    19.     {
    20.         newNavMeshPoint();
    21.     }
    22. // is the finalPosition accessible, not in a building with no access, if so run this function() again
    23.     navpath = new NavMeshPath();
    24.     NavMesh.CalculatePath(transform.position, finalPosition, NavMesh.AllAreas, navpath);
    25.     if (navpath.status == NavMeshPathStatus.PathPartial || navpath.status == NavMeshPathStatus.PathInvalid)
    26.     {
    27.         newNavMeshPoint();
    28.     }
    29. // use for testing
    30.     for (var j: int = 0; j < navpath.corners.Length - 1; j++)
    31.         Debug.DrawLine(navpath.corners[j], navpath.corners[j + 1], Color.red);
    32. }
     
    Last edited: Aug 24, 2015