Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

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.
     
  2. christougher

    christougher

    Joined:
    Mar 6, 2015
    Posts:
    247
    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:
    247
  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:
    247
    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.

     
    DwinTeimlon and The_Widow_Maker like this.
  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:
    65
    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:
    88
    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:
    88
    I was wrong. Please check NavMeshAgent.SetAreaCost
     
  11. TSabos

    TSabos

    Joined:
    Mar 8, 2015
    Posts:
    65
    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