Search Unity

Question Access Unity's pathfinding system

Discussion in 'Scripting' started by RoeeHerzovich, Sep 18, 2020.

  1. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103
    I want to make a fleeing mechanism for my AI.

    I've seen a lot of examples that all include running to the exact other direction. However, this is not a good method because it will also run into objects and would easily be cornered.

    So I thought, Unity uses A* pathfinding, what if I could somehow do that so instead of looking for the smallest F value (if you don't know how A* works please check that out), maybe I can look for the biggest F value, which would potentially make me run away from the destination instead of going to it. The thing is, I cannot access Unity's pathfinding system nor check values for the navMesh.

    I wanted to make my own one, and it's not hard to make an A* pathfinding code, however, a grid doesn't fit my needs and I can't even imagine starting to make a working NavMesh and I doubt I can connect my pathfinder with Unity's NavMesh...

    Is there somehow another way I can do that?
    perhaps there is a way to access Unity's pathfinder that I'm not aware of or an easier way to make a navMesh?
    Maybe Unity's NavMesh can be used with external pathfinders?

    I appreciate your answers :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I don't think your extrapolation of inverting the heuristic would work the way you expect.

    It doesn't really have logical meaning to say "Find the most efficient way to not be here where the player is."

    Instead what you probably want is to select a location that is farther from the player and tell the fleeing enemy, "Go here to get away."

    As to how to generate "farther away" locations, you could use lots of methods. Here's two:

    - have a global notion of "hiding places" evenly spaced, and choose from ones that are "away" from the player from the perspective of the fleeing enemy

    or

    - just randomly choose locations "away" in an ever-larger circle until you hit the navmesh and say "Go here!"

    Obviously for "Away" the start point would be simply inverting the delta to the player and adding that to the enemy.
     
  3. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103

    I thought of that. However, it has a few problems:
    1) Every environment will have different ones you have to manually set up (not that much of a problem just takes time)
    2) It prevents a lot of dynamical behavior. For example, if I want that maybe if I attack some type of NPC it will search for other NPCs of its type in a radius to group up with, I guess it is doable if you first look and if you don't find you go to the points but it's not quite an organized way.
    3) Running to safe spots, even if well-tested depdning on where the chaser is would work amazingly in an open space.
    However, if the fleeing NPC and the Chaser are in a small room, the NPC will try to get out (to the safe point) but unless you take the chaser into account somehow it will just get out of there not thinking of avoiding it.


    I thought of using NavMesh obstacles however the problem with that is that once you make the Chaser an obstcle everyone will avoid them, and not only the runner.

    I tried looking for obstacles to be taken into account only by some agents, but I didn't find one
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Well that's how I would do it. If you have a better idea based on your better knowledge of your specific game's problem space, then by all means go for it. In fact, give the inverted F heuristic a try in your own Astar implementation, but I just don't think it will have a behavior that will get you what you seek, but go prove me wrong!
     
  5. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103

    I'd love to try that, however I just don't know if I can use an external pathfinder such as my own inside of Unity's NavMesh map, I have no idea how to make my own NavMesh, I tried a grid, but it doesn't fit my needs...
     
  6. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    Sound like you need a terrain reasoning layer to teh pathfinding, I suggest you looking for influence map, but for what you want you don't need to invert F, just remove the heuristic (astar become a djikstra) and mark node weight base on distance to player (so lighter node are considered first), basically flying away is a gradient descent issue, ie move to part of the gradient where the weight are lighter. It would be similar to the use of occupency map in video games ai, but inverted.

    The way I would do it, is to use the evaluate function of the navmesh, either at run time select many randomly generated sparse points within the navmesh, around a radius of concern, evaluate path to them, select promising one (based on path distance and path to player distance) and generate more points to redo that until you find one that satisfies condition. The tricky part would be the policy to generate random point so they don't fall back and have good coverage of the space (for optimization, the system would automatically select the furthest one). Alternatively, generate a list of random but equally space point on teh navmesh offline or at loading time).