Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

NavMesh Based Taking Cover AI System

Discussion in 'Navigation' started by tex5564, Jun 15, 2019.

  1. tex5564


    Feb 8, 2018
    I'm building up taking cover system for third person shooter using navmesh.
    My idea is to collect all corners from the navmesh,
    and cast NavMesh.Raycast from each corners to check those are safe or not.
    Collecting vertices from navmesh succeeded like this:
    Code (CSharp):
    1. var navMesh = NavMesh.CalculateTriangulation();
    2. Vector3[] vertices = navMesh.vertices;
    But the problem is, this array includes vertices "inside" polygons.
    Shooting NavMesh.Raycast from inside vertices always returns true,
    so NPC judges those are "safe" spots, and tries to run into the wall.
    Is there any way to exclude those inside vertices from calculation?
  2. ClaudiaTheDev


    Jan 28, 2018
    I am thinking of two solutions:
    Solution 1:
    You can only use box colliders or capsule/sphere/colliders. (Dont work with mesh collider).
    Select the cube in the navigation objects tab and set the setting to "not walkable". When you bake again the nav mesh isnt created inside the cube.

    Solution 2:
    Could be performance heavy.
    You can call the function CanReach on the agent and the vertice position. It should say false to the vertices inside.
    tex5564 likes this.
  3. tex5564


    Feb 8, 2018
    Solution 1 eliminated inside vertices. Thanks!
    Luckily I'm using only box colliders for the buildings(made with Technie Collider Creator),
    and thinking about calculating over 20000 vertices in the field at the same time, solution 1 would be better.
    ClaudiaTheDev likes this.
  4. tex5564


    Feb 8, 2018
    For the people trying to create taking cover AI for shooting games,
    I post this sample scene in the package.

    When you press E, NPC collects all vertices from the navmesh,
    and calculate which corner is safe and able to fire from.
    Positions are stored in CoverPoint class, it has position, normal, and the direction NPC should be covering(Center is desired like covering duck).
    Make sure to set obstacles to "non-walkable" and bake like mentioned above, otherwise character tries to hide inside the obstacles.

    Sorry for rough code, but this is just for sample.

    Attached Files:

    Last edited: Jun 25, 2019
    christougher and Extiward like this.