There are numerous of ways to implement pausing in a game and depending on the game your building your implementation can vary from simple to complex. I’m curious what crazy and interesting ways every one is pausing their game.
So a few questions
How are you pausing the game
Pros and cons of the method
Let me start by getting the some of the most used methods out of the way Time Scale Basic method: Pausing through time Scale is where you set Time.timeScale to zero. This is one of the simplest ways to pause your game but there are some inherent problems with it. For one, if you have animations, say for a menu that depend on time, then you run into the problem of them not animating. I have used this for a temporary quick and dirty pause but have always switched to a different method. The more advanced method of time scale manipulation is to manage your own time which is independent from Time.timescale.
The Lovers in a Dangerous Spacetime did a wonderful write up using this method that you can check here.
Singleton Pause Method: This is where you create a singleton class that holds a flag as to whether or not the game is paused. Then in other scripts that you would like have paused you would use an if( isPaused ) check before the corresponding code. I like this method for really small projects but it annoys me to have to write an the if statement check for each script or part of code i want paused. Also it doesn’t encourage good programming practice/organization since you could literally throw the if( isPaused ) check where ever you want.
There are tons more things that could be argued about each method and lot more can be said about each but hopefully I covered the gist of the core thoughts around them.
I use both… well kind of, it’s not a Singleton, but I don’t just set the timescale down.
Things like Rigidbody simulations and the sort need the timescale turned down so that they don’t simulate. Setting them all kinematic wouldn’t work either as that would screw up their simulation when they reentered simulation.
As for the other part. Well my scenes/levels have a script associated with it. I call it a ‘SceneBehaviour’. It’s where I tell the game to pause, and the SceneBehaviour will be what sets the timescale, and it fires off a signal to everything in the scene that we’re pausing, it then follows that by showing whatever display is supposed to show when paused (pull up a pause menu, black out the screen, whatever).
This signal to everything in the scene will do various things. For instance MOBs all have AI statemachines on them, and they’ll be placed in their ‘paused’ states. The player entities also have a state machine, and they too are placed into the ‘paused state’. It just goes down the line freezing animations and what not. And done.
When unpausing, this is all done in reverse.
Not that many things usually have to have this done to them. It’s mostly just AI entities. Most other things already depend on the timescale (animations and what not) that they’re just dealt with via the timscale hitting 0.
I use the event system/delegates to broadcast a pause and I listen to it with the stuff that I want. I like to keep menus animated with stuff that doesn’t influence the gameplay, so that way of pausing has worked for me so far. It proved to be annoying when pausing scripts where coroutines were running, but there are workarounds.
I completely forgot about Rigidbodies, and your absolutely right. The only alternative is when you set it to kinematic you also store all the relevant data such that you can restore it when the game resumes.
For the projects im working on i have been handling the physics by hand so to speak and just use the rigidbody for collision.
Currently the way I pause a game is through an abstract Pausable class that i wrote which inherits from monobehaviour. Heres the code for the script if any one wants.
//Abu Kingly
using UnityEngine;
using System.Collections;
public abstract class Pausable : MonoBehaviour {
#region Fields
private static bool isPaused;
#endregion
#region Properties
// Getter and Setter
public static bool Paused { get{return isPaused; } set{isPaused = value; } }
#endregion
#region Unity Event Functions
// Update is called once per frame
public virtual void Update() {
if(isPaused) { PausableUpdate(); }
}
// Called every fixed framerate frame
public virtual void FixedUpdate() {
if(isPaused) { PausableFixedUpdate(); }
}
#endregion
#region Methods
// Called once per frame unless isPaused = true
public virtual void PausableUpdate() { }
// Called every fixed framerate frame unless isPaused = true;
public virtual void PausableFixedUpdate() { }
#endregion
}
I really love this method but now that you mention the rigidbody problem im going to have to re-evaluate this method and see if i can get it working with rigidbodies.
Essentially it works where instead of a script inheriting from monobehaviour you inherit from the Pausable class which acts as a sort of middle man. Here let me show you how it works with an actual demo script that will show it in action.
public class Pausable_Demo : Pausable {
#region Fields
private float targAngle;
private Vector3 initialPos;
#endregion
#region Unity Event Functions
// Use this for initialization
void Start() {
initialPos = this.transform.position;
}
// Update is called once per frame
public override void Update() {
// To Pause just call, can be made from any class since Paused is static
Pasuable.Paused = true;
// If you override Update remember to call the base.Update
base.Update(); // Nice thing is you can put this at the end or at the start of your update loop
}
// Used like a regular update, but it will stop updating if the game is paused
public override void PausableUpdate() {
// Moves up and down
this.transform.position = initialPos + this.transform.up * Mathf.PingPong(Time.time, 2);
}
// A regular FixedUpdate but will stop updating if the game is paused
public override void PausableFixedUpdate() {
// Rotates
this.transform.rotation = Quaternion.AngleAxis(targAngle++, this.transform.up);
}
#endregion
}
Notice when you still want to use the normal update function and the PausableUpdate() you have to make sure to call base.Update(). But if you know that you want every thing in a script to stop what its doing when the game is paused you can don’t have to bother with the update function. You can see How the PausableFixedUpdate() does this. Also having scripts that inherent from Pasuable allows for some other interesting things such as finding out if an object is Pausable or not. Don’t how how that might exactly be used but its a cool bonus.
This method isn’t perfect and I would certainly like to have this functionality directly in monobehaviour but for now its been adequate for my projects. Let me know what you think (good and bad) and if you could see utilizing it in a project.
I used a finite state machine for one of my projects but ended up getting quickly frustrated with it (maybe i was using it wrong). It just seemed to make things needlessly confusing and making my code feel really separated. I think the problem was that i didn’t want to have to manage a whole bunch of in and outs from different states for all my scripts.
You can now set an Animator’s Update Mode to Unscaled Time so it’s not affected by Time.timeScale. This way you can pause gameplay (including physics) by settiing Time.timeScale to 0 and still animate your menus.
If this works for your situation, it’s much simpler than the solutions presented above.
Thanks for pointing that out I didn’t even know that it was there. I don’t remember that being there though, did they just recenatly add it in the latest update?