Комментарии:
How do you normally pause your game in Unity? Will you try using events in the future?
ОтветитьTo the nay sayers in comments, this is how you are supposed to pause. Dont be lazy and sloppy take your time to make your game right. Events are efficient and performant and this removes any bugs with pausing from the various other lazy methods.
Ответить"find a script that will be affected by pausing" - like almost all of them?
Do I need to put all this amount of code in every script that is affected by pause?
You are actually teaching here how to create a singleton and use delegates and events. This is not a tutorial of "better pausing technique" in Unity.
ОтветитьDoesn't work, tried to make it work with many and many changes but nothing
ОтветитьAssets\PlayerMovement1.cs(6,14): error CS0101: The namespace '<global namespace>' already contains a definition for 'PlayerMovement'
Assets\InputManager.cs(8,13): error CS0246: The type or namespace name 'Controls' could not be found (are you missing a using directive or an assembly reference?)
Assets\PlayerMovement1.cs(11,10): error CS0111: Type 'PlayerMovement' already defines a member called 'Awake' with the same parameter types
3 Errors but What is these mean?!?!
I've just got some of these codes/scripts on the description.
code link cant open :(
Ответитьi cant reach code links
ОтветитьI thought I was watching a tutorial
Little did I know it was actually a speedrun.
can it be used to slow down specific gameobjects?
ОтветитьHi bro, I just ask one question. What the fuck are you doing?!
Ответитьwhat about things that can't be controlled this way, such as physics?
Ответитьwayyy more complex.....
ОтветитьRight, this is so much more simple and less convoluted... why didn't I think of this?
Ответитьthis is insane lol....I mean this might work for some tiny games, but now you have issues where you're going to have to pause/unpause animations manually, you'll have to freeze/unfreeze physics...it's just an insane amount of work. I'll just tolerate animations resetting themselves
Ответитьdoes this require a subjective pause for each class to listen to pause state or am I missing sth? what happens when state change flag is raised? this looks like complicating things through observer pattern with no actual pause method.
ОтветитьYour code doesn't work, triple checked everything, escape key does nothing with the correct pause controller script attached to the gamemanager object in the scene. Unfortunately, time was wasted.
ОтветитьWhy is there that enable variable on player script?
ОтветитьI can see how this would be a good way to handle where in code to set timescale to 0, but I really don't understand how it could replace timescale. How would this work with dozens / hundreds of different physics objects across a game? Would they all require individual code to be told to pause? What about just simple 2d rigidbody objects with gravity - would we have to turn off gravity for each one individually?
ОтветитьMany people seems angry about this, but i have an opinion that this could be useful, yes many people hate the Singleton but you could replace it, and if you ever played the game called "Sniper elite" You know that there is a lot of camera cinematic shot and slow motion, you could take advantage of disabling player control while manipulating timescale to create slow motion effect
ОтветитьI thought the way to avoid Animator issues was to just set those to unscaled time in the Animator. I mean this also can crop up other issues. I mean this is elegant but I can see some issues trying to apply it to the system I have.
For example in my case I have a weapon controller that likes to know who owns the controller, it can get this one of two ways, 1 from the player or 2 from the weapon it is controlling. If there is no player it will look for a weapon(disabled via this method), but if there is no weapon or player we have an issue. So there are a ton of ways to work around this for example making the weapon controller look for the asset similar to the weapon it cared about but it just seems like a lot when the alternative is a check box in the animator.
Anyway nice video.
how can i do this except animating a camera and let a character do a special move while everithing else is freeze?
ОтветитьI just set the speed of the player to a constant 0 so he can't move XD
Ответитьthis pause system you mention in the video Sounds completed and I don't think it's the best paused system for my game
1. in my I have a lot of different codes I have physics audio animation ECT and i personally don't want to create specific event for each off my code or section my game have this sounds like nightmare to create
2. with using time scale the handle all those stuff and I only have to create pause acception spefic cases
like for example let's say that I have button that appears and disappear a object the time scale don't change that however it's very easy to use time scale and just create acception in specific Case
IMO this is too redundant, you are basically creating a state machine that every single object you want to be afected by pause needs to subscribe to.
For those how don't know unity has actually unscaledTime that means that by using it instead of time the code will run in real seconds and not Unity's time.
For example a coroutine with WaitForSeconds won't work with timescale = 0 but WaitForSecondsRealtime will.
this applies to animations, they have a parameter called update mode that can be set to unscaled time and now the animations will always run even if timescale = 0;
TL;DR just use timescale = 0 and use unscaledTime in your code for the objects/behaviors you don't want to be affected by pause
So complex in just 2 minutes, and it feels sooo clean, amazing work
Ответитьits like wtf, just a messy garbage. You overcomplicated such a simple concept
Ответитьcan you link the code?
ОтветитьWhat is the function of GameStateActiveController Sritpt on the Pause Screen? The code wasnt displayed on the video
ОтветитьInstead of handling pausing per-object manually every time, create an abstract class that inherits from MonoBehaviour called PausableBehaviour. Give it 2 virtual, empty functions called UpdateUnpaused and UpdatePaused. Then, override the Update function (and make it final) to check for a local boolean isPaused, which is set to true/false based on the pause and unpause events. If isPaused is set, make Update call UpdatePaused, else call UpdateUnpaused. Then, make your game objects inherit off PausableBahviour rather than MonoBehaviour, and you will automatically have access to override the PausedUpdate and UnpausedUpdate functions for different functionality without rewriting anything. Or, even better, don't use this terrible tutorial and make a better pause method in the first place.
ОтветитьCODE
Coming Soon
bruh really? it's been over a year!
Okay, I actually wound up copying this because I wound up having a gameplay element where I needed to pause only certain characters, and this served as a useful starting point.
I still use the classic "timescale to zero" method for the normal pause because it will cover everything without having to think about it, but this is useful for selectively pausing objects.
Disliked, apple pie
ОтветитьI'm curious whether this is just something you learned/came up with and are sharing theoretical benefits, or if this is actually something that you have experience with in a larger project and found these benefits to be worth it.
ОтветитьThis is NOT a better way than using timescale. You just had a single system in your game, Movement. In any more-developed game you will end up with lots and lots of different system and many many more classes other than PlayerMovement. Hooking each of them separately will be a nightmare and since you are using events, it will be another nightmare to debug. So, new devs, DO NOT use this method. Simply use the Timescale. More experienced devs won't need an advice, they themselves already know they should not use it.
Also, the reasons why you said using timescale to pause is bad, are not correct but this comment has already become too long.
timescale has more advantages, you can keep animations working, and you can make the timescale interpolate.
ОтветитьTime.scale = 0; will always be better than this. Nice tutorial tho
ОтветитьJust use unscaled time for the things you want to keep running. That is way easier and I never had a problem with it.
ОтветитьWorks the same for Godot. It has an option to set the time scale, but this also interferes with things you don't want paused. This is a good way to control it imho.
ОтветитьWell this is interesting, let me give some useful tipp: in order to have a structured code use empty GameObjects as sort of reference , then you can attach scripts and can establish a architecture . Depending on the scale of the game , you can save those gameobject to file system and load them as per need mode, this would allow to increase performance. Really important question: How many GameObjects should have script as component, well i always try to minimize that and attach scripts to GameObject, only if absolutely necessary, otherwise i dont attach scripts as components. Why? Well then i can easy unit test the scripts ;)
ОтветитьThanks a lot, very useful tip!
ОтветитьHow can you turn off the physics engine?
Ответитьi like this idea. but i think i'll stick to timescale to 0 so i can manually animate specific things like the pause menu to an unscaled time.
Ответитьking
ОтветитьReally appreciate these short very educational videos.
ОтветитьGood way to give yourself a ton of extra work. If you need something (a timer, or animation) to not pause when the timescale is set to 0 there is always a way to use realtime or unscaled time. Animators, DoTween and custom scripts are all easy to do this with, without all this added complexity
ОтветитьWait, am I missing something? Where in the other objects like the player cube object did you actually check whether the gamestate is paused or not and alter their behaviour accordingly? Or did you just skip over that part? Just setting a variable in a singleton doesn't do anything unless all other objects are told how to behave according to how it changes. And I guess that's the reason why people use the timescale, because they notice at some point that they want to pause and timescale allows to do it without adding a parent state to all other gameobjects.
ОтветитьИбануцца ИГОРЬ. С десятаго раса понъял!
ОтветитьNice info, thanks.
Ответить