Search Unity

Get exact future position and velocity of NavMeshAgent

Discussion in 'Navigation' started by gamecreatorc1, Jun 5, 2022.

  1. gamecreatorc1

    gamecreatorc1

    Joined:
    Dec 12, 2019
    Posts:
    64
    I would like to know exactly where a NavMeshAgent will be at a future time and what its velocity will be. I've looked at the documentation and I don't think there's a solution with any of the NavMeshAgent methods. Searching online provided the following possibilities:

    The closest thing that you're able to get is corners (aka path) but between them who knows where the agent will be? You can estimate the position with a line between two corners but if the agent rounded a corner at a high speed, he'll travel along a curve, not a straight line. And hills along the way will also slow down and speed up the agent, making math predictions difficult. So corners are no good.

    Someone claimed to use steeringTarget but that seems to have the same problem of relying on corners so I don't know how that would work.

    Next hopeful idea was to simulate multiple steps/frames of the NavMeshAgent. I don't believe this is available in Unity. You can do Physics.Simulate but that doesn't seem to touch the NavMeshAgent.

    Related to the above, last possibility I saw was to increase Time.timeScale to simulate multiple Updates. This seems to work but I'd have to write a lot of code around it. It may also slow down the program with a lot of agents and other stuff going on. Edit: I realized that this wouldn't work for me because I'd need to apply movement to character controllers during the time I want to skip... which means I can't skip it. Too bad.

    So, what's the best way to do this? I'm hoping I'm just missing something obvious.
     
    Last edited: Jun 5, 2022
  2. Tion-Gaming

    Tion-Gaming

    Joined:
    Jan 30, 2015
    Posts:
    28
    Hey, been through literally just that myself. I know the pain and let me tell you, there is NO documentation for it at all, I cant even find the source code for the Agent. Check out my question https://forum.unity.com/threads/how...ts-slowdown-curve-to-stop-at-a-point.1291388/

    There I actually show graphs of how they move and provide the equation for the starting curve. If you dont recognise the equation, look up "Equations of motion" or "Kinematic formulas". You can calculate all of that as you were asking, the only thing you cant predict with what I have in my question is their position/velocity just before they reach the end because thats what my entire question is asking about lol.
     
    gamecreatorc1 likes this.
  3. gamecreatorc1

    gamecreatorc1

    Joined:
    Dec 12, 2019
    Posts:
    64
    Thank you. What a bummer that this isn't part of Unity. I'll take a look when I get a chance. Really appreciate you posting that here.

    In the meantime, any other leads are still welcome...
     
  4. Tion-Gaming

    Tion-Gaming

    Joined:
    Jan 30, 2015
    Posts:
    28
    Ive figured it out. I even created a desmos graph of an agents velocity over distance. Δx is the total distance from the start point to the end point. r is the agents radius, s0 is the speed, a0 is the acceleration. This works only for a straight line case, and assumes the agent starts with no velocity. if you want it such that the agent starts at a specific velocity, you need to change the 0 in the functions to a variable.
    https://www.desmos.com/calculator/mda8ppydyr
    From that you can calculate their velocity at any distance from the starting point. I again refer you to the kinematic equations if you wanted to calculate time or acceleration, you will just need to re-arrage the desmos equations.
     
  5. gamecreatorc1

    gamecreatorc1

    Joined:
    Dec 12, 2019
    Posts:
    64
    Props and thanks for sharing that. I'm not good at math so I was exploring the possible NavMeshAgent functions but it turns out you can't do several steps in one frame, per the documentation (NavMeshAgent.velocity).
     
  6. Tion-Gaming

    Tion-Gaming

    Joined:
    Jan 30, 2015
    Posts:
    28
    Im not sure what you mean by steps in a frame, but the change in velocity over time is constant. While the agents velocity magnitude is smaller than the value of speed, it increases the velocity by the acceleration for the time that has passed (Time.deltatime). Once the agents velocity has a magnitude equal to that of the speed, then that is how many m/s they will go until they are decelerated one way or another. You can raycast ahead using Physics.Raycast or you can raycast the navmesh using NavMesh.Raycast to detect collision ahead of time. Given their distance from the destination you can also know when they will start to decelerate to stop at the destination (it starts at the distance form the point equal to the agents radius times 2 (diameter)). Given all of that you should have a decent way to predict the agents movement, you just run the calculation for the current frame, use the velocity change and the distance from the current velocity to have a prediction of where they will be next frame and at what velocity, use that to calculate the frame ahead of that. And with any good approximation, use the agents position and current velocity compared to what you predicted it would have been at that point to adjust your calculations. To work on some of that stuff you can just google "unity how to find a distance between two points" or "unity how to find a magnitude of a vector", Ill give you a hit, both are already in unity. its Vector3.Distance() and SomeVectorVariable.magnitude. There are no functions to predict the agents built into unity so doing your own predictions is the best way to do this sadly. A programmers greatest tool is their ability to find the answers they need, not their ability to know the answers already.