Pause Your Game the Easy Way! (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys today we're going to look at a really simple way to pause and resume a game we'll demonstrate using this scene that we created in our 3d platformer series it has various moving and animated components that we'll be able to pause and resume at the press of a button if you want to you can download this project by supporting us on patreon all the links can be found in the description okay so the first thing we'll do is go to the scripts folder in the assets panel we'll create a new script and name it pause controller we'll drag this onto the character in the hierarchy to assign it then we'll double-click the script to open it in the editor in here we need to keep track of whether the game is paused or not we'll create a private boolean field named is paused then in the update method we'll check if the escape key has been pressed if it has we want to toggle the pause state we can do this using the not operator this will set is pause to true if it was previously false and vice versa now we need to take some action based on the pause state we'll check if it's paused if it is we want to freeze everything the easiest way to do this is by setting the time scale to zero this will effectively stop time in the game will now be zero in every frame so any movement or animation that depends on this will be stopped we'll add an else statement to do the reverse and set the scale back to 1 when the game is unpaused let's save the script and switch back to unity to try it out now when we press escape the game pauses when we press escape again the game resumes and we can carry on one thing to be aware of with this method is that setting the time scale to zero does not stop the update methods in your script executing so for example input can still be processed let's pause the game and press the space bar normally this would make the character jump nothing happens while the game is paused but if we resume we can see the character is now playing the jumping animation and we're in a bit of a strange state this is because the input handling code was still executed and changed the animation state while we were paused let's stop the game and switch back to the script to fix this we're going to add a couple of unity events that will fire when the game is paused and resumed we need to add the unity engine events namespace then we'll add an event called game paused and another chord game resumed then in the update method we'll invoke the game paused event when the game is paused when the game resumes we'll invoke the resumed event now we have a way of notifying when the pause state changes let's save this and switch back to unity we'll select the character in the hierarchy then in the inspector we'll add a handler for the game paused event we'll drag the character into the object slot now we can find the player movement script in the drop-down and select the enabled property we'll leave the checkbox unticked now whenever the game paused event fires the player movement enabled value will be set to false we'll do the same thing for the game resumed event this time we'll tick the checkbox to enable the script again let's press play to give it a try now when we press space while paused it has no effect and we can pause and resume without any unwanted side effects one final thing we'll look at is what if you don't want everything to pause so you may want your pause screen to be animated or you may want some things to still go on in the background luckily unity has the concept of unscaled time which allows you to ignore any changes to time scale let's stop the game and have a look at an example let's say for some reason we wanted the character animation to continue playing while paused if we go to the animator on the character we can change the update mode to unscaled time this will cause the animation to always play at a time scale of 1 regardless of what the time scale is set up let's press play to try this out now when we pause the character carries on animating there's an equivalent unscaled time for other things as well for example in your scripts you can use time dot unscaled delta time instead of time dot delta time one thing to be aware of though is that fixed update is never called when the time scale is set to zero so if you need to use the physics system when paused then you'll probably have to go down a more manual route for the majority of cases though using time scale should work fine okay that covers everything for this video hope you found it useful a big thank you to all our patrons we really appreciate you helping to support the channel if you'd like to help and also get access to the source code you can find details in the description please leave any questions or feedback in the comments and subscribe and click the bell icon so you don't miss the next one thanks guys
Info
Channel: Ketra Games
Views: 9,502
Rating: undefined out of 5
Keywords: Pause Game Unity, Pause Unity, UnscaledTime Unity, UnscaledDeltaTime Unity, Pause Game Unity 3D, Pause Unity 3D, UnscaledTime Unity 3D, UnscaledDeltaTime Unity 3D, Unity, Unity Tutorial, unity3d, Unity3d Tutorial, Learn Unity, Indie Game Development, Game Development, Learn Unity3D, Unity 3d, Unity Tutorials, unity 3d Tutorial, Tutorial, Game Development Unity, Unity Game Tutorial, Unity Tutorial for Beginners, Unity Beginner Tutorial
Id: qboX1FBxIHE
Channel Id: undefined
Length: 5min 51sec (351 seconds)
Published: Wed Aug 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.