Search Unity

How to wander with respect to Area Cost

Discussion in 'Navigation' started by mrCharli3, Oct 21, 2019.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I am using my own Wander Script that uses SamplePosition to find a position to go to, for some reason this doesnt always return a position on the NavMesh but I can live with that for now.

    Code (CSharp):
    1. protected bool SamplePosition(Vector3 position)
    2.         {
    3.             NavMeshHit hit;
    4.             return NavMesh.SamplePosition(position, out hit, navMeshAgent.height * 2, NavMesh.AllAreas);
    5.         }
    I am wondering if I can get this to respect the area costs somehow? so that if there is a road they will pick a random point on a road instead of a random point in rough grass for example.

    Much like this video: https://arongranberg.com/astar/docs/images/random_point.mp4

    In this video it doesnt just walk around the high cost area, it actually chooses it less times as a destination too, which is what I want.
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Not sure if I understood you correctly, but couldn't you sample the environment with areaMask so that you just pick spots on the preferred areas, like roads?
     
    Last edited: Oct 21, 2019
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Well, that would make it so it never picks a spot on the grass EVER, I want it to use the cost system. So its just 90% more likely (for example) to go on the road.
     
  4. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    SamplePosition does not use the cost system, so no that is not possible. But you can easily circumvent this buy using sample SamplePosition for every specific layer and do the random picking yourself.