Search Unity

Setting Timescale to > 1 has odd effect on agent.velocity

Discussion in 'Navigation' started by mrCharli3, Oct 23, 2019.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I am making a management game and want to enable fast forwarding, I do this using Time.timeScale.

    I have this piece of code that manages the Blend tree for my idle/walk/run animations on my agent:

    Code (CSharp):
    1. void Update()
    2.     {
    3.             float speedPercent = (agent.hasPath ? agent.velocity.magnitude : 0f) / agent.speed;
    4.             animator.SetFloat("speedPercent", speedPercent);
    5.     }
    however, I notice here that agent.velocity.magnitude does not always return 0 when standing still if timescale > 1. For some reason agent.velocity gets a y-value that is above 0, which is odd cus Im testing on flat ground, and never change the y-value of the agent.
    The agents x and z velocity stays at 0 when still, as expected. Is this a bug? Or is my code above wrong?

    I simply want my speedPercent to be 0 if my agent is standing still.
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Does the y-value consistently return the same number, or does it fluctuate?
    Either way, a simple workaround is to simply calculate a vector2 magnitude using the velocity's x and z components.
     
  3. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300

    I have played around with Navmesh and Timescale alot and there are a lot of problems due to overshooting, stopping distance, velocity etc. What I reocmmend always is to disconnect simulation from view. Which means have a navmesh agent doing the work for you. Then take the position and interpolate/manipulate your view to it. So you have full control of how your view is looking even though the agent might glitch around because of too high timescale speeds.