• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by WiS3 · Sep 13, 2015 at 04:22 PM · c#networkingraycastnavmeshnavmeshagent

Click to Move - get NavMesh Area Name

I'm creating a Click to move C# script, that use the built in Pathfinding using navmesh and NavMeshAgent.

My script currently works perfectly, but i want to do a important thing for the game i'm developing.

This is the current script that i have.

 public bool CanMove = true;
 private NavMeshAgent nav;

 void Start(){
     nav = GetComponent<NavMeshAgent>();
 }

 void Update () {
     bool RMB = Input.GetMouseButtonDown(1);
     bool reachedDestination = nav.remainingDistance <= float.Epsilon;

     if(nav.isActiveAndEnabled && RMB && CanMove) {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ray, out hit)) {
             string tag = hit.transform.tag == null ? "NULL" : hit.transform.tag;
             Debug.Log ("Hit: " + tag);
             targetPosition = hit.point;
             reachedDestination = false;
         }
     }

 if(nav.isActiveAndEnabled && CanMove && !arrived && targetPosition != null){
     nav.destination = targetPosition;
 }

Using this, if i for example click on a Unwalkable area of the terrain (a mountain for example), my characters move toward that point and stop on the maximum walkable area of the NavMesh.

What i need, is to check the Area of the NavMash, so if it is Walkable then i set the navMeshAgent.destination, else, i do nothing, or show a visual error that the character can't walk there.

Also, i need this because i will have multiple NavMesh Areas, for example, WalkableTerrain, SwimableWater, and so on.. and when the player try to move i need to know the area that he want to go.

So the question is, How i can modify my script to get the navMesh Area Name where the player Right Clicked?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ash-Blue · Apr 17, 2016 at 07:15 AM 0
Share

Did you ever figure this out? I too would love to know how to get this at run-time. Looks like you can only retrieve the value with editor class GameObjectUtility.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Ash-Blue · Apr 17, 2016 at 07:18 AM

You cannot access this at run-time as of 5.3. You'll have to pre-cache and store the names somewhere via GameObjectUtility.GetNavMeshAreaNames() or reference the areas by ID. Horrible solution and hopefully this information is made available at run-time.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Happy-Zomby · Apr 17, 2016 at 08:13 AM

Hi, you can use sample position to get the mask http://docs.unity3d.com/ScriptReference/NavMesh.GetAreaFromName.html

If you do a first classic raycast - you can then use that hit point to do your sample position

    if(NavMesh.SamplePosition(physicsHit.point, out navmeshHit, 1.0f))
 

and then use

 navmeshHit.mask

When I use that with agent.sampleposition - I got power of 2 results but I was able top work with that

 switch (navHit.mask)                                                                //check area mask and change speed in function
     {
     case 8:
         agent.speed = 7/mod;
         break;
     case 16:
         agent.speed = 6/mod;
         break;
     case 32:
         agent.speed = 5/mod;
         break;
     case 64:
         agent.speed = 3/mod;
         break;
     case 128:
         agent.speed = 0.5/mod;
         break;
     }

hope that helps

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ash-Blue · Apr 26, 2016 at 12:08 AM 0
Share

Raycasts are fairly expensive although you could cache the response. On the other hand comparing against potentially changing IDs is incredibly error prone. If it was comparing against string names this could be a really great solution. As mentioned in my post though, the corresponding names for the IDs are locked away in the editor unless you post-process them into a cache.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Navmesh get rotation 1 Answer

My ai is getting stuck when there is a lot of them 0 Answers

Distribute terrain in zones 3 Answers

How Do I Make A NavMeshAgent Move To Points Sequentially? 0 Answers

NavmeshAgent.remainingDistance remains at 0 but the agent still moves and behaves correctly 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges