Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

How to detect if NavAgent has reached hit.point? (2) Distance doesn't work? 1 Answer

NavMesh Modifier not changing navmesh 2 Answers

NavAgent not following on rebuilt NavMesh 0 Answers

NavMeshAgent object jumps 0 Answers

SetDestination's and CalculatePath's Path State Return Works Too Late 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges