Search Unity

Possibly noobish question about AI, NavMeshAgents and altering paths on the fly for avoidance.

Discussion in 'Navigation' started by Tset_Tsyung, Aug 8, 2017.

  1. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Hey all,

    First post in this section, so hope I don't break any rules (usually stick to the newbie forum).

    JUMP TO BOTTOM FOR THE QUESTIONS THEMSELVES, THE REST IS CONTEXTUAL PRE-RAMBLE.

    Basically I have AI monsters which do nothing but use their own NavMeshAgents to close in on the player.
    I have a player which is actually AI controlled (it's a learning environment for me to learn AI).

    Now, the problem is that the 'player' cube primitive (I'll just call him "Mannie" from now on) has a tendency to just run through the mobs to reach his target as per his NavMeshAgent.

    I can't add a NavMeshObstacle to the mobs as this will conflict with the NavMeshAgent on them. I COULD implement a different path finding system for them (I will prob do this at some point as part of my learning - just not right now) and this would allow me to use NavMeshObstacle... but looking for an alternative.

    I have start writing a 'detour' system that check for mobs in the path, and then create a waypoint to cirumnavigate the mobs. The problem with this is that I can't find a way to add this waypoint to the NavMeshAgent.path... unless I'm missing something really simple...

    So, my questions are as follow:
    1) Is there an actual way to adjust the path's corners manually? Perhaps copy them, add waypoints, reinsert?

    2) Failing that is there another way I can trick the NavMeshAgent to avoid the Mobs without adding NavMeshObstacle or writing my own simple pathfinding for the mobs (they're essentially zombies...)

    3) Any other thoughts for this noob, who is very willing to read, study and learn (just a little misguided at times)

    Many thanks,



    Mike
     
  2. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    Unity's navmesh agents have a local avoidance system. If everything is setup correctly, they should at least try to avoid running through each other. If this is not working, some of your settings might be off.

    But I guess you want to make sure the player does not cross the path of the mobs at all. This will not work with the local avoidance. You can still achieve this by giving the mobs a NavMeshVolume. When they walk, they will alter the navmesh accordingly and increase the cost of the current sourroundings. The increased cost will prevent that the player will cross their path. Therefore you need to rebake/update the navmesh frequently.

    If you do not know how this works, please checkout the examples of the LocalNavMeshBuilder:
    https://github.com/Unity-Technologi...ssets/Examples/Scripts/LocalNavMeshBuilder.cs

    Please note that the LocalNavMeshBuilder currently works only if it's a simple navmesh with a few NavMeshModifers/NavMeshVolumes. Bigger meshes with lot's of object are quite slow to update. To avoid performance problems you can increase the navmesh voxelsize (Navmesh is using lesser triangles) or split up your navmesh and multiple meshes and use navmesh links to connect them.

    Let me know if you have any further questions or need help with the LocalNavMeshBuilder example.
     
    Tset_Tsyung likes this.
  3. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Dwin,

    Many thanks for the reply. I will go and start reading up on that now. Will try to implement it and get back to you asap with the result. Many thanks.

    I was also thinking of possibly stopping the navmeshagent just long enough to run my own local avoidance script, then starting it again (with new path) to continue it's journey after it's clear of danger... but will def try your method first BEFORE I start making things harder for myself, lol.
     
    DwinTeimlon likes this.
  4. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Dear Dwin,

    Okay, so I've been playing around with these components and am having some issues - I was hoping to get some input...

    The short of it is that the player is still ploughing into the mobs. But here's a more indepth explanation of the state of play.

    1) I have a 'ground' plane. Apart from the "NavMeshSourceTag" script, do I need anything else, such as a "NavMeshSurface"? And should I bake the navmesh initially in the editor? Please note that including the "NavMeshSurface" script in my project is casuing errors and won't compile. Am I missing another component from the repository?

    2) Any walls and solid obstacles (a grand total of 4 objects, 3 of which are primitive cubes) have a "NavMeshObstacle" component attached to them with 'Carve' ticked - for some reason the NavMeshModifierVolume script did nothing...

    3) Despite having the NavMeshModifierVolume on the mobs there is no visible adjustment of the navmesh in the editor (this is also the case for the 'static' obstacles like walls - see point 2 above), a screenshot of this is in the next post.

    4) I found this article (https://forum.unity3d.com/threads/update-navmesh-at-runtime.475237/) and in there he mentions 'at runtime add NavMeshSourceTag to your dynamic generated content.', I've tried do this, but still no prize.

    Here are some screenshots of my settings for your convenience.



    I'm am trying to set the modifier with this code when spawning mobs.

    Code (CSharp):
    1. if(newMonster != null)
    2. {
    3.     navModifier = newMonster.AddComponent<NavMeshModifierVolume>();
    4.     navModifier.AffectsAgentType(0);
    5.     navModifier.area = 4;
    6. }
    Please Note: Upon spawning, the mob will have area set as 4, however the "AffectedAgents" will still be set with 1 element with the value of -1 (the default). But then again, I think I've not quite understood this field properly - looking at the source isn't too helpful either, especially us the "AffectsAgentType" returns true :(

    Finally, what I'm seeing in my editor is very different to what's in the (understandably thin) documents, is this correct?

    Sorry to be barraging you with these questions. Hopefully I'm not being too helpless and annoying here...
     

    Attached Files:

    Last edited: Aug 9, 2017
  5. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Okay, can't edit last post and go to more options atmo... But here's a pic of what the navmesh look like whilst the project is running - notice the lack of visual feedback from the mobs' "NavMeshModifierVolume".




    A thought has just occured: Am I supposed to call a function to force the navmesh to rebake, or does it do it automatically after so many frames or movement of mobs?

    Update: Moving the empty game object with the "LocalNavMeshBuilder" script on it up and down a little every few second doesn't do anything (although, moving it 10 vertically causes some of the navmesh to disappear... not important.)
     

    Attached Files:

    Last edited: Aug 9, 2017
  6. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    Hmm it seems you haven't yet grasped the concept of the LocalNavMeshBuilder. My best advice would be to dig into the example code first. NavMeshComponents/Examples/Scenes and I think it's the modify_mesh example. Don't just browse over it, disect that project until you understand what is going on.

    You definitely don't need the NavMeshSurface, the LocalNavMeshBuilder will have you covered.

    Every object which influences the Navmesh needs the NavMeshSourceTag. This tag indicates that this objects does change the NavMesh and the LocalNavMeshBuilder is looking for those. Please also make sure to add those tags in your prefab already. LocalNavMeshBuilder collects the information of all tags and alters the NavMesh automatically.

    As I can see in your screenshots your monsters do not have any influence on the navmesh, otherwise it would be colored differently. This indicates that the navmesh is not recalculated at all. Additionally the layers are not 0,1,2,3 but 1,2,4,8,16.... Either work with the layernames or use the correct layer numbers.
     
    Tset_Tsyung likes this.
  7. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Dwin,

    Many thanks for your help so far, and for the direction to look at the actual example scenes. I've been doing this all morning, and feel that I have a better understanding of how the individual components SHOULD work. However I'm still having trouble getting the 2 modifier components to work properly - or at all for that matter (such a noob :( ).

    First I will say that anyone reading this should copy over the GitHubs's navmeshcomponents/Assets/NavMeshComponents folder in it's entirety so that you get the editor scripts as well, this will help with not only making what you add to your objects look exactly as they do in the docs, but also enable easy 'area' selection.

    But this bring me onto the issue I'm having. The NavMeshModifier and NavMeshModifierVolume scripts don't have any effect on the nav mesh at all. I've even tried messing with a new scene AND the scenes from the example project.

    For example:
    I create 2 primitives, raise them above the main plane, give them a source tag and, hey presto, they have their own nav mesh (which is expected). However, when I give one or both of them a NavMeshModifier, set it to "Override" (as per the docs) and set the area type to "Not Walkable" or another custom area there is no change, visually or behaviourally. They just keep on rolling over the mesh of the primitives as per normal.

    Any Mesh Object with a source tag when raised just enough through an already existing navmesh, will carve itself out of said already existing mesh. This had caused an issue with my mobs where are constantly noticing they are not on a navmesh, teleport to the nearest point only to carve another area out of the navmesh, notice they are not on a navmesh, teleport to the nearest point... etc. etc. They end up looking like a teleport hopping drunk driver on ice.

    The NavMeshModifierVolumedoesn't do anything to the navmesh either (visually or behaviourally). Like, at all - I just cannot get this to do anything.

    As stated above I've tried messing around with my project AND the downloaded copy of the NavMeshComponents project, the results are always the same.

    If anyone has had ANY success using the modifiers in anyway I'd love to hear from you. In the meantime I'm going to continue looking for as much documentation as I can scrape together on using these components... but so far it's pretty bleak.

    Google, don't fail me know!!!

    P.S. I'm so sorry if it feels that I'm wasting your time Dwin, I'm honestly trying, just not succeeding. Again I really appreciate all the correspondance you've given me so far and completely understand if I've driven you to despair.
     
  8. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    It is really hard to judge what you are doing wrong. If you can provide a small sample project with the problems you have, I would be ok to spend a few minutes looking into it and give you some hints.
     
    Tset_Tsyung likes this.
  9. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Hi Dwin,

    Sorry for the late reply, but got there in the end.

    Here's an example scene that show's the issue:
    https://github.com/TsetTsyung/MikesIssues

    There is an object called "Populator", this spawns in the badguys (they do nothing but patrol up and down like the goodguy).

    However even though they have the ModifierVolume and source tags on them they don't affect the navmesh. There is also a badguy2 prefab which has the cube mesh on the same object as the sourcetag - this one jeks about in the aforementioned fashion.

    The goal is to have my goodguy go around the badguys so that they can't 'touch' him, however as it stands he brushes against all in his path...
     
  10. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    NavigationTest.zip

    I have used Unity 2017.1p2

    One of your mistakes was that the NavMeshSourceTag does only include meshes on its hierachy, but you put the component one hierachy above. Another trick is, that the mobs and the player now use different navmeshes.
    I have updated the NavMeshSourceTag code (did this for my project anyway), it is now collecting NavMeshVolumes, Modifers and only Meshes in all children.

    It is working, but unfortunately it's far too slow. It costs already 15ms on a quite fast machine in such a small setup. As the navmesh needs to be rebaked every time a mob is moving, this will only get slower. You could change the frequency of the backing, but it wouldn't help.

    If you want to get this working you probably need a different pathfinding solution (apex path, has dynamic obstacles), not sure about A*, but the backing is definitely much faster in A*.

    Your idea switching the NavMeshAgent off, doing your own avoidance and switching it on again, does also not work imho. What you could do is, let the path calculate from unity (NavMesh.CalculatePath) and do the path following on your own with your own avoidance or buy some of those packages:

    Dynamic Obstacle Avoidance http://u3d.as/NiB
    Polartih AI Pro http://u3d.as/Aeh

    You could also look into this steering lib:
    https://github.com/ricardojmendez/UnitySteer
     
    Last edited: Aug 11, 2017
  11. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Dear Dwin,

    Funnily enough, I did think of editing the scripts whilst putting together that sample project (noticed having mobs with different navmesh caused issues leading me to think they weren't getting their own navmesh at all)... but that scared me (which I shouldn't, I know...) so I thought I would wait for your reply - glad to know I was in the right ball park at least.

    You have been so helpful with this issue - I'm so chuffed you read this post.

    As for your comments about switching solutions or doing it myself I will certainly go down this route and experiment a bit.

    I started this project ot learn more about different AI architectures and approaches (Well, I say more... I know nothing, lol) so tearing this apart to learn it is on the ToDo list anyway.

    If I magically stumble upon a solution that works I'll update this thread (or start a new one to stop necro-ing).

    Again, many thanks for your help - you're a star!
     
    DwinTeimlon likes this.