Search Unity

Different navmesh layer costs for different NPCs

Discussion in 'Navigation' started by The_Widow_Maker, Sep 8, 2017.

  1. The_Widow_Maker

    The_Widow_Maker

    Joined:
    Nov 21, 2014
    Posts:
    10
    Hi all

    I have a problem where I want different NPCs navmesh agents to have different navigation costs for the same navigation area.

    e.g. I would like soldier characters to try to avoid high cost areas such as mud and water if a better alternative route is available, but I would like zombie characters to ignore the cost value and just run straight rough terrain etc.

    Any help would be appreciated, thanks.
     
    Tymianek likes this.
  2. christougher

    christougher

    Joined:
    Mar 6, 2015
    Posts:
    558
    Sounds like you'd want two different agent types with separate Nav meshes. Should be easy enough, just a little extra workflow. Are you using the new (5.6) Nav Mesh system?

     
  3. The_Widow_Maker

    The_Widow_Maker

    Joined:
    Nov 21, 2014
    Posts:
    10
    hi, thanks for your reply.

    I am using Unity 2017.1.0F3, So I think I will be using the new nav mesh system.
     
    Last edited: Sep 8, 2017
  4. christougher

    christougher

    Joined:
    Mar 6, 2015
    Posts:
    558
  5. The_Widow_Maker

    The_Widow_Maker

    Joined:
    Nov 21, 2014
    Posts:
    10
    These examples are excellent but not quite what we are after, what we are after is for different agents to have different navigation costs.

    e.g. Water for a soldier would have a navigation area cost of 8 while water for a zombie would have a cost of 1, the desired effect would be that the zombie would ignore all rough terrain cost and just pick a direct route to his target even if its slower.

    Again, we are very thankful for your help.
     
  6. christougher

    christougher

    Joined:
    Mar 6, 2015
    Posts:
    558
    So... lets say you have your player nav mesh... which uses a navmesh agent type of humanoid. When you use a NavMeshSurface to bake it you can have navmeshModifierVolumes.cs target only that agent type's navmesh with the "Affected Agents" setting. Then you need a second agent type "zombie." Then with a second navmeshSurface component you bake a zombie agent navmesh that doesn't get affected by the navmesh modifiers... Should work without any custom code or anything.

    Important... don't bake the navmesh using the Navigation Window. This is now the legacy way to bake. Use the NavMeshSurface.cs component. In this case you will use it twice.

     
  7. The_Widow_Maker

    The_Widow_Maker

    Joined:
    Nov 21, 2014
    Posts:
    10
    Thanks again Chris, you have been very helpful.
     
  8. TSabos

    TSabos

    Joined:
    Mar 8, 2015
    Posts:
    94
    Is there no way to do this without multiple navmesh surfaces?

    I am wanting some complex AI to change their behaviour at runtime like a thief or guard may go down a dark alley where as a normal peasant may take a longer way around. The AI may decide heck with it and go down the alley though, so there needs to be some way to identify a cost and adjust it for that agent would be the ideal solution.

    Dangerous area = 10 cost, but right now to this agent it is instead 3 would be the idea. I'm sure I could change the agent type at runtime (maybe?), but that isn't as robust a solution.
     
  9. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    If you want to use automatic, asynchronous pathfinding with the NavMeshAgent there is no other way around this. If you just use the CalculatePath function and do the rest yourself it would probably work if you adjust the cost of the layer before calling it.
     
  10. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    I was wrong. Please check NavMeshAgent.SetAreaCost
     
    Tymianek likes this.
  11. TSabos

    TSabos

    Joined:
    Mar 8, 2015
    Posts:
    94
    I forgot I posted this question and also found SetAreaCost. It is amazing, allows me to set a Guard->Dangerous area to cost 1 and a peasant etc to cost 40 just at night time. :D
     
    Tymianek, DgoodingIndi and JBR-games like this.