Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Flanking maneuver?

Discussion in 'Navigation' started by MaximumTre, Aug 30, 2020.

  1. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    Hello :)

    I'm working on an AI in a third person game that I want to make a flanking movement to get behind the player and I'm wondering if anyone has done this with the default nav agent? So far, I'm thinking I should parent some transforms to the player that are always to their rear and make the enemy AI go towards those first before attempting to approach the player from behind, but I wonder if there is a more elegant way to do this? Any help is appreciated!
     
  2. Skynet766

    Skynet766

    Joined:
    Sep 13, 2020
    Posts:
    22
    I can tell you what didn't work for me. I tried having the AI "look" for corridors to the left and right, and move into them rather than move right towards the player. The problem? They would sometimes run into a passage on the left right, then path towards the player... and run back into the room and charge the player directly. That would happen because the shortest path to the player was not through that corridor.

    If you must use transforms, you need to make sure your flanking route will actually go around the player. Say there is a table to the right of the player. It may look like a good flanking route, but then the Navmesh system makes the AI run right up to the player's face to get to the table. Not good.

    Personally, I've written my own pathfinding every time I need flanking. You can trust your own AI to avoid the player, because it can support things like "I should move here because the player isn't looking here".
     
    MaximumTre likes this.
  3. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    I've been busy working on other parts of my project that I didn't even think about something that simple. Make the AI move to a point the player isn't looking at was kinda my aim, but I didn't think to use the player's current view in variables the AI can use to determine where to move. I also encountered that same problem you mentioned, using set transforms means the NavAgent will recalculate and sometimes just beeline towards the player to get to the point behind the player. At this point, I think I should just use the NavAgent to get near the player, then write my own script or use and FSM to handle the actual flank maneuver.
     
  4. Skynet766

    Skynet766

    Joined:
    Sep 13, 2020
    Posts:
    22
    Even if you don't want to completely write your own pathfinding, you can still get the Navmesh triangles and do some analysis to help your AI. Something I used to do was get the sides of the Navmesh triangles and do raycasts to see if that side could be used as cover:
    - Would it protect the AI up to waist height e.g. table, fountain?
    - Could the AI still stand/crouch fire (there isn't a full wall in the way)?

    That cover data can help you pick safe places for the AI. But there is one problem: you still need to know where the AI and the player are in terms of Navmesh triangles. So you need to be able to efficiently work out which triangles everyone is in. I use a Octree, here's my implementation I use for Unity https://www.nuget.org/packages/OctreeUnity

    You need to write a class to represent the triangles e.g. NavmeshTriangle. Then, you can find whatever triangles you need. You query by bounding box e.g. find the triangle your player is on by finding which triangle overlaps with their bounding box.

    Want to find which cover points are near the AI? Make a big box centered on the player and see what cover is in the nearby triangles.

    At this point you've actually done 50% of the effort of writing your own pathfinding. The two hardest things about writing your own NavmeshAgent are:
    - Finding what triangle the agent and it's destination is in without destroying the framerate (you've just done this)
    -Turning the set of triangles you need to walk through into a decent path http://digestingduck.blogspot.com/2010/03/simple-stupid-funnel-algorithm.html

    I'm debating open-sourcing my own "NavmeshAgent" somehow, a lot of navigation questions seem to come down to "I need the AI to move through the Navmesh in a special way"
     
    MaximumTre likes this.