- Home /
How to pause and unpause my game - Input.GetAxis doesn't work when Time.timeScale is 0
I don't seem to be getting any keypress responses to Input.GetAxis() while the Time.timescale is set to zero.
When I set the timeScale to 0.2 (for example) instead of 0, it works fine - so the zero timeScale is definitely the issue.
For example, I have set up a Pause key input - if I press it the game pauses (timeScale = 0) but when I press it again nothing happens (the keypress is not detected).
I have tried setting timeScale to 1 before the GetAxis call and 0 again after but it still doesn't work.
Using Input.GetKeyDown is not an acceptable workaround because there are other keys I want to be able to press while the game is paused, keys that must be definable in the settings.
Answer by duck · Oct 12, 2010 at 09:30 AM
When Time.timeScale is set to zero, none of the normal "Update", "FixedUpdate" and similar functions are called, so this is probably the source of your problem - your entire Update function is just not being executed so any code inside will appear to not work.
You can get around this problem by implementing your Pause code in a coroutine, which does not use a Time-based function for the return delay, like this (a c# example) :
public class PauseControl : MonoBehaviour {
void Start()
{
StartCoroutine(PauseCoroutine());
}
IEnumerator PauseCoroutine() {
while (true)
{
if (Input.GetKeyDown(KeyCode.P))
{
if (Time.timeScale == 0)
{
Time.timeScale = 1;
} else {
Time.timeScale = 0;
}
}
yield return null;
}
}
}
Although it's true that FixedUpdate is not called when Time.timeScale is zero, the same is not true for Update, which is still called.
In fact, if I map the axis (in the Input $$anonymous$$anager) to the mouse scroll wheel ins$$anonymous$$d of a keyboard key, it is detected just fine in the Update loop and unpauses as expected.
Your example does not work for me because it relies on Input.Get$$anonymous$$eyDown. If I wanted to use Get$$anonymous$$eyDown I could use it in my current implementation just fine, no coroutines required. However I must use Input $$anonymous$$anager axes, which are still not even detected when placed in a coroutine.
The problem is not the update loop/coroutine, the problem is that GetAxis returns zero for keyboard presses when Time.timeScale is zero.
Strange - I made a quick example project here before posting the code, and Update() is clearly not being called when TimeScale is zero for me (I have a Debug.Log call in it, so I can tell).
However you're right about Input.GetAxis not giving values when paused, however you shouldn't need to use "GetAxis" to read key presses, even User-Defined ones. $$anonymous$$aybe you need to use "GetButton" or "GetButtonDown" ins$$anonymous$$d?
Not sure about fixed update but Update() works fine in when time =0. The most common mistake is to depend on delta time in some lerp, and this is why most code don't work.
Answer by tucano · Nov 29, 2011 at 02:13 PM
My actual workaround is this one. When "manager" is in pause, Time.scale is 0.0f
if (manager.pause) {
key_x = Input.GetAxisRaw("Horizontal") / keyMoveSpeed;
key_y = Input.GetAxisRaw("Vertical") / keyMoveSpeed;
} else {
key_x = Input.GetAxis("Horizontal") * Time.deltaTime * keyMoveSpeed;
key_y = Input.GetAxis("Vertical") * Time.deltaTime * keyMoveSpeed;
}
Seems that AxisRaw is collected also when Time.scale = 0, notice that it returns 1, -1 or 0 only from keys. That (for testing) I just divide for the keyMoveSpeed constant (is ugly I know)
This should be considered the right answer, as the slected one is wrong and doesn't adress the problem. It seems like a bug, probably the smoothing process of axis uses time or delta time somewhere.
Answer by tucano · Nov 29, 2011 at 02:02 PM
My actual workaround is this one. When "manager" is in pause, Time.scale is 0.0f
if (manager.pause) {
key_x = Input.GetAxisRaw("Horizontal") / keyMoveSpeed;
key_y = Input.GetAxisRaw("Vertical") / keyMoveSpeed;
} else {
key_x = Input.GetAxis("Horizontal") * Time.deltaTime * keyMoveSpeed;
key_y = Input.GetAxis("Vertical") * Time.deltaTime * keyMoveSpeed;
}
Seems that AxisRaw is collected also when Time.scale = 0, notice that it returns 1, -1 or 0 only from keys. That (for testing) I just divide for the keyMoveSpeed constant (is ugly I know)
Your answer
![](https://koobas.hobune.stream/wayback/20220613075406im_/https://answers.unity.com/themes/thub/images/avi.jpg)