Search Unity

Question What is the standard way to pause a game when activating a popup menu?

Discussion in 'Editor & General Support' started by IIBearWithII, May 28, 2023.

  1. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    For example, in a book I'm studying, I'm writing a FPS game that, when you press "M", a menu pops up that allows you to adjust the volume. But the character's view still moves around when you move the mouse, and shooting still happens when you click the mouse to adjust the volume or mute it. I know I can just add a Boolean variable that only allows the methods for my main game to play if the menu isn't activated. But I thought I'd just ask if there was a more elegant way of dealing with this, or if there is some standard way that I haven't been taught yet...
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,442
    Making a global flag is not entirely out of the question.

    Many Unity games freeze the passage of time. Set Time.timeScale to zero, and anything that uses Time.deltaTime (any physics, Transform.MoveTowards speeds, etc.) will pretty much freeze in place.

    There are gotchas in this approach: anything animated in your menus now needs to work with "unscaled time" or it freezes too, anything that divides by Time.deltaTime (to calculate velocities for example) will suddenly be getting math errors, and anything that doesn't use Time.deltaTime at all will just keep on trucking.
     
    IIBearWithII likes this.
  3. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    I did time scale last time.
    I check if Time.deltaTime is zero in the beginning of void Update() if it is I return immediately.

    And it is not elegant way, elegant way allows you to scale subsystems or states in fsm, but it is hard to do in unity.
     
    IIBearWithII likes this.
  4. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Thanks! Glad to hear my approach wasn't horrible, but also glad to be given that Time.timeScale option to consider.