Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Stop multiple Agents while Game is paused

Discussion in 'Navigation' started by Omni_shambles, Feb 29, 2016.

  1. Omni_shambles

    Omni_shambles

    Joined:
    Feb 29, 2016
    Posts:
    2
    Hi folks,

    I am currently trying to develop a game with Unity to learn C# and the Engine. It is a lot of fun but now I’ve ran into a problem which I could not solve by myself and even not by searching on google (yet). So I would like to ask you for your help:

    So here the little backstory:

    I have a NavMesh and I have several defined spots (represented as empty GameObjects) stored in a list. Every 3 seconds a NPC spawns at a defined SpawnPoint, searches for a free spot in the list and sets this as a target. The spot is then marked as “taken” so that the next NPC cannot get the same spot as a target and has to pick another. Once a spot is set as a target the NPC will move towards his spot until he reached it.

    I now wanted to implement the feature to pause the game. The NavAgent should stop if the game is paused and resume its path when the game carries on:

    Code (CSharp):
    1.     // Update is called once per frame
    2.     void Update() {
    3.  
    4.  
    5.         // IF a free spot was found and not yet reached
    6.         if (foundSpot == true && reachedSpot == false) {
    7.             // IF the game is NOT paused
    8.             if (!PauseMenu.Instance.Paused) {
    9.                 // Walk to the spot!
    10.                 Walk();
    11.                 // Or resume if you stopped
    12.                 agent.Resume();
    13.             }
    14.             else {
    15.                 // If the game is paused: STOP!
    16.                 agent.Stop();
    17.             }
    18.         }
    19.     }
    It works perfectly except in one situation. At the time the first NPC reached his spot all the other NPCs will not stop anymore when the game is paused. I was not able to figure out why this happens. I thought maybe it is because all NPCs are instantiated from the same prefab. But I believe every NPC is its own running instance. So if the first NPC reached the spot reachedSpot ist set to true. But on all other NPCs it is still set to false. Basically all other NPCs should still be reacting on the if-else code I posted.

    Maybe someone has an idea or maybe a hint?

    I would really appreciate your help

    Best wishes

    Omnishambles
     
  2. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    707
    First Im assuming the code you posted is on every agent .
    Just a guess but maybe when the first agent's spot is reach it is triggering all the agents to reachedspot = true; maybe check each agent while game is paused to see if this changed on the agents.
     
  3. Omni_shambles

    Omni_shambles

    Joined:
    Feb 29, 2016
    Posts:
    2
    Hey, thanks for replying. Yes the code is on every agent. I had the same thought but I already checked it: It is not the case. reachedspot changes on every agent seperately.
     
  4. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,006
    The right way to handle pause is Time.timeScale = 0 (if this crashes on you, try 0.0001 but I think they fixed that bug)