Search Unity

The best way to approach navmesh obstacle

Discussion in 'Navigation' started by Leonel73, Feb 25, 2018.

  1. Leonel73

    Leonel73

    Joined:
    Jun 4, 2017
    Posts:
    10
    Hey.
    I'm trying to make rts game. Buildings in the game have NavMeshObstacles with carve component. Units have NavMeshAgents. I want my units to be able to attack buildings. Technically: unit calculates path to nearest to him point near building's collider, goes there and attacks. So the question is: what is the best way to make it?

    I can't set destination to building because path to it is invalid. I tried to use NavMesh.SamplePosition, but it doesn't fit my task because it finds only one point and all units try to go there instead of surrounding building.

    I had an idea to set destination according to unit's direction relative to building, but it doesn't consider obstacles on unit's way and often happens that real nearest point is on another side of building.

    Sorry for my English, i hope the problem is clear.
    Any help will be appreciated.
     
  2. havchr

    havchr

    Joined:
    Jun 18, 2009
    Posts:
    75
    You could get a random point on a sphere - Code taken from https://forum.unity.com/threads/solved-random-wander-ai-using-navmesh.327950/


    Code (CSharp):
    1.  
    2. public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask) {
    3.         Vector3 randDirection = Random.insideUnitSphere * dist;
    4.         randDirection += origin;
    5.         NavMeshHit navHit;
    6.         NavMesh.SamplePosition (randDirection, out navHit, dist, layermask);
    7.         return navHit.position;
    8.     }
    9.  
    However - I guess what you should figure out - is how units will behave when ordered to go to the same spot - or how they should act when "surrounding" --..

    Is it possible that a building might hold some transforms that are "parking lots" - where attacking units go to when attacking - and that each time you pick a parking lot for a unit - then it gets "taken" - so next unit must go to the next parking lot. Not sure if that made any sense, but hope it gave you some ideas.