Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

How to know the area name that agent is stepping?

Discussion in 'Navigation' started by IntDev, Apr 7, 2015.

  1. IntDev

    IntDev

    Joined:
    Jan 14, 2013
    Posts:
    152
    The agent go by many areas, like walkable, street, etc. How can I know the area it is currently stepping?
     
  2. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    Use the NavMeshAgent.SamplePathPosition api:

    Code (csharp):
    1. // SampleGround.cs
    2. using UnityEngine;
    3. #if UNITY_EDITOR
    4. using UnityEditor;
    5. #endif
    6.  
    7. [RequireComponent (typeof (NavMeshAgent))]
    8. public class SampleGround : MonoBehaviour {
    9.    NavMeshAgent agent;
    10.    NavMeshHit hit = new NavMeshHit();
    11.  
    12.    void Start () {
    13.      agent = GetComponent<NavMeshAgent> ();  
    14.    }
    15.    
    16.    void Update () {
    17.      // sample directly under agent
    18.      agent.SamplePathPosition(NavMesh.AllAreas, 0.0f, out hit);
    19.      Debug.Log ("Agent is currently on " + hit.mask);
    20.  
    21. #if UNITY_EDITOR
    22.      // in editor show the name too
    23.      var names = GameObjectUtility.GetNavMeshAreaNames ();
    24.      for (var i = 0; i < names.Length; ++i)
    25.      {
    26.        int mask = 1 << NavMesh.GetAreaFromName(names[i]);
    27.        if ((hit.mask & mask) != 0)
    28.        {
    29.          Debug.Log (".. that's \"" + names[i] + "\"");
    30.        }
    31.      }
    32. #endif
    33.    }
    34. }