Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by bigbat · Jun 15, 2013 at 11:57 AM · ainavmeshnavigationnpc

How to get a random point on NavMesh?

Hi to all,

In my current project i use unity navmesh for ai navigation.i want my npc's be able to get a random walkable destination(point) on navmesh and flee to that point when he/she see the player. Any help or tips on how to do it, I'd be grateful.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
24
Best Answer

Answer by Valkyr_x · Jun 17, 2013 at 04:17 AM

This might not be the best way to do it but it worked for my purpose.

I generated a random point with in a unit sphere and multiplied it by the max distance I wanted my agents to walk:

 Vector3 randomDirection = Random.insideUnitSphere * walkRadius;

I then added my agents current position to this vector and used the NavMesh.SamplePosition function to find the close position on the NavMesh:

 randomDirection += transform.position;
 NavMeshHit hit;
 NavMesh.SamplePosition(randomDirection, out hit, walkRadius, 1);
 Vector3 finalPosition = hit.position;

I then used final postion as the destination for my agent.

Hope this helps!

Comment
Add comment · Show 5 · 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 bigbat · Jun 17, 2013 at 08:57 PM 0
Share

It works perfectly and my agents can move freely in scene,of course i modified it a few to fit with my script. thanks a lot.

avatar image PvTGreg · Dec 17, 2014 at 09:17 PM 0
Share

sorry to bump an old thread but what functions do you suggest i use theese in

avatar image Marks981 · Jan 03, 2015 at 02:47 PM 0
Share

Hello guys,maybe im too late,but i just want to say,im soo lost,how can i use this script,please put full script?!?!? I dont understand those lines,please put full script with functions and everything,these lines just makes my head hurt. i need to ai flee away from me,but these lines are soo incomprehensible ,PLEASE just finish this f script. Thanks :)

avatar image vexe · Jan 06, 2015 at 05:59 PM 1
Share

@$$anonymous$$arks981 what don't you understand? If there's any specific point you're not sure about, read the docs about it. If that didn't help, try reading what others say about it. If that didn't help, feel free to ask about it :)

avatar image SeasiaInfotechind · Mar 17, 2015 at 05:56 AM -1
Share

Hello bigbat,

I just wrote the code, hope it will help.

         InvokeRepeating("GameObject_ChangePosition", 0.0f, 1.0f);
 
     void GameObject__ChangePosition()
     {
         GameObject_targetPoint = new Vector3(Random.Range(-8.0F, 8.0F), 0, Random.Range(-4.5F, 4.5F));
     }
 
     void Update () 
     {
 `            if(GameObject.GetComponent<Nav$$anonymous$$eshAgent>() != null)
                 GameObject.SetDestination(GameObject_targetPoint); `}
avatar image
3

Answer by Selzier · Oct 27, 2017 at 04:56 PM

Thanks for the answer Valkyr_x! Expanding on that here's a C# function for getting a random Vector3 Navmesh Position:

 public Vector3 RandomNavmeshLocation(float radius) {
         Vector3 randomDirection = Random.insideUnitSphere * radius;
         randomDirection += transform.position;
         NavMeshHit hit;
         Vector3 finalPosition = Vector3.zero;
         if (NavMesh.SamplePosition(randomDirection, out hit, radius, 1)) {
             finalPosition = hit.position;            
         }
         return finalPosition;
     }


And this can make the NavAgent move by calling this code, with whatever radius you want:

 myNavAgent.SetDestination(RandomNavmeshLocation(4f));
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 Igorotak · Dec 12, 2017 at 09:44 AM 0
Share

can you please explain what the 4f is for? is it the radius or is it time?

avatar image CrowbarSka Igorotak · Mar 11, 2018 at 10:42 PM 0
Share

It's the "radius" argument in the first line:

 public Vector3 RandomNavmeshLocation(float radius)
avatar image idbrii · Oct 21, 2020 at 10:56 PM 0
Share

Beware that this may make an agent that's near the edge of the navmesh run to the origin! (If radius is > 1 and it picks a point more than 1 away from the navmesh, it returns Vector3.zero.)

Better to return transform.position or a Vector3? so the caller knows when it failed (they'd probably want to retry).

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

24 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

Related Questions

NavMesh.SamplePosition returns point outside the NavMesh 0 Answers

nav mesh agent not following properly 0 Answers

Way to find closest point in a specific area on navmesh. 0 Answers

CS0246: The type or name space name ''NavMeshSurface'' plz some help 0 Answers

Navmesh Agent "move back" when player comes to close? 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