• 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 bhavinpanara22 · Dec 14, 2014 at 09:10 AM · navigationarea

How to check if my gameobject is created on navigation baked area ?

Hello,

How can i check if my gameobject is created on Navigarion baked area ? I mean, i have baked my environment using navigation. Now, i have to instantiate enemy prefabs around some radius of my player.I am using random to create enemy prefabs. So, how can i make sure that, my enemy will be created on that navigation baked area( that blue area ).

Thanks.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Mmmpies · Dec 14, 2014 at 10:29 AM

Not used this myself but NavMesh.SamplePosition should help.

 private NavMeshHit myNavHit;
 private float maxDist = 10f;
 
 NavMesh.SamplePosition(Vector3 to check, out myNavHit, maxDist, 1 << NavMesh.GetNavMeshLayerFromName("Default"));
 
 Debug.Log(myNavHit.position);

Haven't tested the code but it should return the nearest NavMesh position to the Vector3 to check position that you pass it.

You could be really strict with the maxDist and set it to 1 or below if you want the point to be forced really close to your random location.

EDIT:

OK I haven't used this but as I understand it you give is your Vector3 and it finds the closest point on the NavMesh that is within the maxDist float.

Just try it and Debug.Log to output what it returns. I think myNavHit will be a bool with 1 if it finds somewhere and 0 if it doesn't and myNavHit.position will be the position on the NavMesh if it finds one in the range.

But you're 100% correct the documentation is really poor. Had to Google to put that example together.

Anyway the command breaks down to this:

Vector3 to check == your enemies randomly generated position

myNavHit == the returned value and position

maxDist == the maximum distance from your random position to check for a good NavMesh hit

1 << NavMesh.GetNavMeshLayerFromName("Default") == the Default NavMesh, change the word Default if you use another NavMesh

EDIT 2:

Just tried it on a random movement script I have and the original post had an error this code works:

 NavMesh.SamplePosition(target, out myNavHit, maxDist, 1 << NavMesh.GetNavMeshLayerFromName("Default"));
         
         Debug.Log(" myNavHit = " + myNavHit + " myNavHit.position = " + myNavHit.position + " target = " + target);

Note target is a Vector3 like your random enemy position.

So I was wrong about the bool but it does provide a good NavMesh position.

and this is the output in the console:

alt text


navmesh.png (5.3 kB)
Comment
Add comment · Show 3 · 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 bhavinpanara22 · Dec 14, 2014 at 10:35 AM 0
Share

Can you please explain, what this will exactly do ? There is not enough information on unity docs also.

avatar image netvortex_dc · Dec 14, 2014 at 01:17 PM 0
Share

When you place your NPCs you use SamplePosition() to get a position that is actually ON the Navmesh. In the example shown his NPC is at "target" but the Nav$$anonymous$$esh returns the position myNavHit.position - which is the position on the actual navmesh.

avatar image Mmmpies · Dec 14, 2014 at 01:46 PM 0
Share

That's right and as you can see from the figures I returned the X and z were fine it just wasn't happy with my y value of 13.0 replacing it with 14.3

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

Add user-defined Navigation Area through script? 0 Answers

how can I detect when gameobject hit a navigation area? 1 Answer

How can I get the pre-defined color of a NavMesh area ? 2 Answers

Making NavMesh areas? 0 Answers

Change nav mesh area of a Nav static object via code?? 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