Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How do i get the int-value of a navigation agent type

Discussion in 'Navigation' started by manuelgoellnitz, May 23, 2017.

  1. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    366
    I have more than one agent types and want to change the agentTypeId of an agent during per script.
    The AgentTypes are defined as Strings e.g. "Humanoid" (the default one).
    The variable which stores the information in the gameObject is Integer.

    Is there a way to get the Integer-Value out of the String-Value added in the Editor?

    The only way I found so far was to set The agents agentTypeId to my custom ones and then switch the inspector to "Debug", which showed me the value. But that seems to be a very odd way to do that.
    And the value itselöf is odd too: Humanoid = 0, NewOne1 = -1372625422
     
  2. Jakob_Unity

    Jakob_Unity

    Unity Technologies

    Joined:
    Dec 25, 2011
    Posts:
    269
  3. PabloNoBrakesGames

    PabloNoBrakesGames

    Joined:
    Mar 19, 2019
    Posts:
    4
    Other workaround is to access the NavMeshSurface from the inspector from your custom agent script and then access to navMeshSurface.agentTypeId
     
  4. trzy

    trzy

    Joined:
    Jul 2, 2016
    Posts:
    128
    Annoyingly, there does not appear to be a function that maps string name to agent type ID. But you can write one like this:

    Code (csharp):
    1.  
    2.     private int? GetNavMeshAgentID(string name)
    3.     {
    4.         for (int i = 0; i < NavMesh.GetSettingsCount(); i++)
    5.         {
    6.             NavMeshBuildSettings settings = NavMesh.GetSettingsByIndex(index: i);
    7.             if (name == NavMesh.GetSettingsNameFromID(agentTypeID: settings.agentTypeID))
    8.             {
    9.                 return settings.agentTypeID;
    10.             }
    11.         }
    12.         return null;
    13.     }
    14.  
    This is needed if dynamically creating nav mesh surfaces. In my case, I'm creating nav meshes from room plan information obtained from an AR system. I have an inspector field that lets me specify the agent type by name and then I do:

    Code (csharp):
    1.  
    2.                int? agentTypeID = GetNavMeshAgentID(name: _navMeshAgentName);
    3.                MeshRenderer meshRenderer = anchor.GetComponentInChildren<MeshRenderer>();
    4.                 if (meshRenderer != null)
    5.                 {
    6.                     NavMeshSurface surface = meshRenderer.gameObject.AddComponent<NavMeshSurface>();
    7.                     if (agentTypeID != null)
    8.                     {
    9.                         surface.agentTypeID = agentTypeID.Value;
    10.                     }
    11.                     surface.layerMask = _navMeshLayerMask;
    12.                     surface.AddData();
    13.                     surface.overrideVoxelSize = true;
    14.                     surface.voxelSize = 0.1f * surface.voxelSize;   // fix the offset from surface that occurs with the new NavMesh system
    15.                     surface.BuildNavMesh();
    16.                 }
    17.  
     
    tonemcbride likes this.