Search Unity

NavmeshAgent SetDestination doesn't work properly with TimeScale 0

Discussion in 'Navigation' started by SnowblindFatal, Oct 13, 2019.

  1. SnowblindFatal

    SnowblindFatal

    Joined:
    Jun 9, 2014
    Posts:
    20
    Hello.

    I have a script which causes a character to follow the mouse cursor. When timescale is 1, everything works fine and the path gets updated constantly. However, when I set timescale to 0 (since I intend to have a kind of planning phase during which the player can issue commands to his character), the SetDestination function works for a few frames but then stops doing anything until I set the timescale back to 1.

    Here's a video where I demonstrate this behaviour. I am using keyboard shortcuts to set timescale to 1 and 0 periodically:


    What's going on here? Why does the NavmeshAgent stop calculating new paths? The destination field of the NavmeshAgent remains unchanged when I call the SetDestination function.

    There are no messages in the console, errors, warnings or anything. It fails completely silently.

    Using Unity 2019.2.8f1 on Windows 10, 64 bit.
     
  2. SnowblindFatal

    SnowblindFatal

    Joined:
    Jun 9, 2014
    Posts:
    20
    It appears that when timescale is set to 0 and I try to SetDestination on a NavMesh "zone" different than the current target's zone, it stops working. Again, a video to demonstrate:
     
  3. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    382
    That's just the nature of TimeScale, it's used pretty much everywhere in Unity's systems ranging from pathfinding and following to physics calculations.
    Depending on why you're changing the TimeScale, it might be better to remove that system and create something from scratch that interfaces with the systems you need to slow down/speed up.
     
  4. SnowblindFatal

    SnowblindFatal

    Joined:
    Jun 9, 2014
    Posts:
    20
    How strange. I wouldn't have imagined that time needs to pass in order for Unity to do pathfinding calculations. If this is by design... OK I guess? I'll have to go through all my objects and ensure they don't do anything during the planning phase. I feel like I'm just asking for trouble by this. :D

    Thanks for the reply!
     
  5. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    382
    It's not using it for the path calculation as far as I'm aware (but I could very much be wrong about that), but it's using it for actually walking over the path. If you set timescale to 0, simply no time will pass so your objects can't move along a path.
     
  6. SnowblindFatal

    SnowblindFatal

    Joined:
    Jun 9, 2014
    Posts:
    20
    Doesn't seem to be that way. I'd specifically like the navmeshagent to just plan the route during this planning mode with timescale set to 0. Or maybe the planning algorithm simulates walking the agent across the trip and needs time for that? Doesn't seem to make much sense, overall, or I'm just not getting something.

    I think I'll try the A* pathfinding library.
     
  7. SnowblindFatal

    SnowblindFatal

    Joined:
    Jun 9, 2014
    Posts:
    20
    Yeah, using the A* pathfinding package seems to work much better. I submitted a bug report for this problem, but I guess it doesn't matter much now. Not going back to using Unity NavMesh at least in this project.