Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by Topthink · Nov 21, 2016 at 10:00 AM · randomnavmeshagent

How do I change Nav Mesh Agent Speed with c# Script ?

I have several "Nav Mesh Agents" in my program and I want to assign certain variables randomly.

In the Inspector/NavMeshAgent, I know how to set my agents Speed and Acceleration manually. And I know how to create random numbers using Random.Range(min, max)...no problem there.

I simply cannot find the unity/C# syntax to assign random values to those attributes.

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
3
Best Answer

Answer by Dibbie · Nov 21, 2016 at 10:08 AM

I would take a look at the documentation for NavMeshAgent, it will cover all the functions you can use by script.

So you can set the speed at random, with an idea like GetComponent<NavMeshAgent>().speed = Random.Range(0.1f, 10.0f); as an example.

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 Topthink · Nov 21, 2016 at 07:33 PM 0
Share

Thank you kindly. I think I tried about everything I could think of but, as it turns out, I didn't try what you have posted there. It Works Great!!!

I really appreciate you taking the time to help me.

I read the documentation but I will read it again. I'm not quite experienced enough (yet) to always fully understand what I read the first time.

Again, thanks.

avatar image
0

Answer by bojantube · Mar 01, 2020 at 06:31 AM

@Dibbie how do i change values to stop distance? this.GetComponent().stoppingDistance = 0.001f; how?

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 SonOfTheDevil · Feb 01 at 03:11 AM 0
Share

Hi @bojantube

There are a few different things you can do:

  1. You can change it in the Inspector, by opening the NavMeshAgent component, and lowering the stopping distance to the $$anonymous$$imum it will go which is 0.

  2. You can change it in your script to 0 for Mathf.Epsilon. (Mathf.Epsilon is a ridiculously low float number, the smallest number you can do. i.e. 0.000....1)

  3. You can cache it is the awake function. The reason why you want to cache it in Awake is because Awake() gets called before Start() does, and it will stop the having to use :

_exampleObject.GetComponent<ExampleComponent>();

When GetComponent is called way too often, it can create lag and actually cause the game to run a lot slower than cacheing the objects in Awake()

Cache other things, like object references in there as well:

( Using 'this.blahblah' in this matter is considered "Redundant". I would use 'this.blahblah' only if this script is on multiple objects)

 private void Awake()
             {
                 _exampleAgent = this.GetComponent<NavMeshAgent>();
                 _examplePlayerObject = GameObject.Find("Player");
             }
 
 private void ExampleMethod()
             {
                 _exampleAgent.stoppingDistance = Mathf.Epsilon;
              }

I can't get the code section to work, sorry about that lol

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

61 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 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

first random positon is always the same 2 Answers

Can you randomise a Navmesh agents rotation 1 Answer

Add randomness to Navmeshagent 0 Answers

[SOLVED] NavMeshAgent Create Imperfect Paths? [SOLVED] 1 Answer

AI pathfinding waypoints 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