The right way to pause a game in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're making a game in unity then chances are you're going to need a way to port it this might be to show a message access inventory or change the game's settings whatever it is the reasons why you might need to pause the game are probably pretty obvious to you but what's the best way to do it luckily it is a simple option the easiest way to pause the game in unity is by setting the game's time scale to zero this basically stops all time based logic which effectively pauses the game while setting the time scale to 1 again unpauses it while there are other methods of pausing a game in unity pausing with time scale is generally the easiest and most convenient way to do it it suspends most of the systems you would typically want to pause in a game or from a single line of code so much so that in many cases setting the time scale to zero to pause your game may be all you need to do however while most operations will be stopped when using this method not everything in the game is affected and some systems might not behave in the way you'd expect them to in this video you'll learn how pausing the game with time scale works how to control what does and doesn't get paused as well as how to avoid some easy to make mistakes when pausing your game in this way so how does it work time scale is a public static value of the time class that will affect how time-based logic is processed in your game changing the time scale value to anything other than its default of 1 will speed up or slow down time so for example a timescale value of 0.5 will slow the game down to half speed while a value of 2 will double it this can be useful for creating slow motion effects or for speeding up time in a simulation for example while setting timescale to zero stops time completely effectively pausing the game then when you want to unpause the game simply set the value back to its default of 1. the timescale value is static meaning that it can be accessed via the time class from any script in the game what this also means is that the time scale value won't reset when a new scene is loaded meaning that if you use timescale to show a pause menu and then use the pause menu to return to the main menu unless you set the time scale back to 1 time will still be paused when you get there but how does changing the time scale actually change the speed of your game when you create movement or any kind of logic in your game that's based on time you will typically have to scale the speed of that movement by delta time which is the amount of time that's passed between the previous frame and the current one as you may already know this is because a game's frame rate is always variable meaning that in order to do something consistently over a number of frames you will usually need to scale whatever it is you're doing by delta time for example multiplying an object's movement vector by delta time converts its movement speed to units per second instead of units per frame while adding up delta time over a number of frames allows you to measure time accurately as your game runs no matter what the frame rate is the time scale value modifies delta time causing any logic that is scaled by it to run faster slower or if you set time scale to zero not at all pausing the game meaning that so long as the time based logic in your game is scaled by delta time as it will be in most cases setting the time scale to zero will pause it when using the timescale method you could argue that the game isn't really paused at all instead nothing is happening because time isn't moving forward objects can't move time can't be measured and as a result the events of your game are effectively stopped but that doesn't mean that nothing's happening behind the scenes so what does and doesn't get paused when you set time scale to zero in unity it might surprise you to know that when the game is paused update will still be called this means that anything in an update loop including in co routines will continue to run regardless of the time scale unless it's time based this means that if you've scaled something by delta time or you're using a time measured instruction such as wait for seconds in a co-routine invoke or the optional delay when destroying an object it will be affected by time scale and will be suspended when pausing the game in this way which is useful as it means the timing of events in your game will be paused and resumed correctly along with everything else fixed update which is typically used to interact with the physics system does not get called when the time scale is set to zero when time is faster or slower than one fixed update gets called more or less frequently to match creating slower or faster physics however when it's zero it simply doesn't get called at all this means that physics objects simply don't move when the game is paused and anything inside of the fixed update event even if it's not time based won't be called either time.time which is the time in seconds since the game started will be stopped when time scale is set to zero in most cases this is a good thing as just with other time based functions it means that any in-game measurement that relies on time will also be paused correctly but what if you still need to measure time when the game is paused while you'll probably want to pause most of the things in your game there will no doubt be some objects scripts or functions that you don't want to stop such as menu animations for example so how can you work with time when time isn't moving forward unscaled time in unity is time that is unaffected by time scale changes which can be useful for excluding specific time-based operations from being paused when the rest of the game is for example while time.time is affected by time scale time dot unscaled time isn't when the game is paused delta time will be zero but unscaled delta time will be unchanged while unscaled fixed delta time will report the actual amount of time between fixed update calls which might be slower or faster than normal depending on the time scale put simply whenever time is used you'll probably also find an option to use unscaled time instead allowing you to exclude certain objects and functions from being affected by time scale changes if that's what you need for example in a co routine you can use wait for seconds real time in place of wait for seconds which can be useful if you're using a co routine to animate your menu or if you're using animation the animator component will allow you to change its update mode from normal to unscaled time and if you want to move an object simply use whatever function you'd normally use and scale it by unscaled delta time instead of delta time however while you may need to manually exclude certain objects and scripts from being paused there are some parts of your game that by default will not be stopped when the time scale is set to zero audio for example will continue as normal even when the game is paused individual audio sources can themselves be paused if they're playing however managing all of the audio sources in your game like this could be tricky and kind of defeats the point since one of the biggest benefits of pausing the game in this way is that you can easily stop everything without having to go object to object luckily there's a way to pause all of the audio in your game easily setting audiolistener.pause to true will pause all audio sources from a single call and if you don't want to pause a particular audio source such as one that's used for menu music and sounds you can set it to ignore the audio listener pause allowing it to play the last thing that you might want to check for when pausing your game are changes in state that will take effect after the game is unpaused such as input for example input checks when using unity's original input manager at least are often done in update which is still called when the game is paused this means that an in-game control can be triggered in a pause menu even though the game has been stopped typically the action you trigger won't happen while the game is paused since many of the things a player can do are probably going to be scaled by delta time anyway but what can happen is that as soon as the game is unpaused the player may jump shoot or perform an action that was triggered while the time scale was zero as a result if you are using update-based input checks in your game it can be a good idea to keep track of whether or not the game is actually paused and disable in-game input when it is alternatively you may find it helpful to create an event that informs other objects where the game is paused or unpaused allowing you to turn certain systems on or off as you like in response for more information on creating events in unity try my video on events and delegates which i've linked in the video description now i want to hear from you are you pausing your game by setting the time scale to zero or are you using a different method and what have you learned about pausing the game in unity that you know someone else will find helpful whatever it is let me know by leaving a comment like this video if you found it useful and get subscribed for more videos from me i'll see you next time [Music] [Music] you
Info
Channel: Game Dev Beginner
Views: 47,661
Rating: undefined out of 5
Keywords: unity
Id: ROwsdftEGF0
Channel Id: undefined
Length: 9min 9sec (549 seconds)
Published: Fri Jul 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.