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
0
Question by castor · Nov 12, 2015 at 09:36 PM · navmeshnavmeshagentdistancetimescaleprecision

Precise distance calculation with high timeScale for NavMeshAgents?

So here is my situation: While agentA goes toward agentB, and if the distance between them is less than 0.8, agentA and agentB will stop. agentB can also be moving and both game objects are NavMeshAgents actors moving by setting destination using Unity's NavMesh system. Here is the code I'm using to calculate the distance:

 function Update(){
    if( Vector3.Distance(agentA.position, agentB.position) < 0.8)){
       //Stop both objects
    }
 }

Now, everything works great until I increase the timeScale. The higher the timeScale, the more agentA will 'miss' the proper 0.8 distance from agentB. I'm assuming that the reason why this happens is because the Update() function runs less times per frame and so it lacks precision once the timeScale is increased.

Is this correct? And if so, how can I make it so that agentA will stop at the right distance independently from the timeScale?

Comment
Add comment · Show 2
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 kingcoyote · Nov 12, 2015 at 09:51 PM 0
Share

Have you tried calling FixedUpdate ins$$anonymous$$d of Update? FixedUpdate runs independently of the framerate. It might not work in this situation, but it's worth a shot.

avatar image castor kingcoyote · Nov 12, 2015 at 09:56 PM 0
Share

Just tried it and it makes no difference. FixedUpdate is only used for Rigidbody calculations, right? Not sure how it could help in this situation.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Sep 20, 2016 at 09:47 PM

Well, the problem is not that Update runs less often, but since you increased the timescale the movement steps get larger. Usually Time.deltaTime accumulates to "1.0" after one second. If you set the timescale to "2.0" deltaTime will accumulate to "2.0" after one realtime second. Likewise the gametime (Time.time) will also advance 2 seconds for every realtime second.

Since the steps get larger it has a similar effect as running on a low framerate. As solution you can use "super sampling". This should be possible by setting updatePosition to false for your agent and manually update the position each frame. nextPosition will give you the target position that your agent want to reach this frame. So you have to move it manually to this position. However instead of just moving it there in one step, you would use a loop and do it in several steps.

 var agentAagent : NavMeshAgent;
 
 function Update()
 {
     var superSampleCount = 2*Time.timeScale;
     var delta = 1f / superSampleCount;
     var oldPos = agentA.position;
     for(var f = 0f; f < 1.0f; f += delta)
     {
         agentA.position = Vector3.Lerp(oldPos, agentAagent.nextPosition, f);
         if( Vector3.Distance(agentA.position, agentB.position) < 0.8))
         {
             // Stop both objects
            agentAagent.nextPosition = agentA.position;
            break; // break out of the loop to stop in place.
         }
     }
 }

This assumes that this script actually runs on "AgentA". As you can see each frame the agent will simply move to it's target position for the current frame, but it moves in smaller steps. The number of steps increases as Time.timeScale increases so the result should be more or less the same.

By default i let it perform two cycles per frame. So at timeScale =2 it will perform 4 cycles.

Note: It's important that you break out of the loop when you want to stop the agent at the current position.

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 castor · Dec 08, 2016 at 09:45 PM 0
Share

What if I wanted to use a similar solution just to prevent an agent from over shooting when going from A to B? (independently from going to a target?)

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 prevent Navmesh Agent from overshooting destination with high Time.TimeScale? 1 Answer

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

Find closest "item" object by NavMesh navigation distance performantly 0 Answers

Can we use Navmesh for Player without relying on SetDestination ? 0 Answers

How many nav mesh agents can I have in a scene ? 2 Answers


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