Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by bobthefish · Oct 12, 2010 at 08:40 AM · inputcoroutinepausetimescale

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;    
     }
 }

}

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bobthefish · Oct 12, 2010 at 11:37 AM 0
Share

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.

avatar image bobthefish · Oct 12, 2010 at 11:37 AM 0
Share

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.

avatar image duck ♦♦ · Oct 12, 2010 at 12:18 PM 0
Share

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).

avatar image duck ♦♦ · Oct 12, 2010 at 12:22 PM 0
Share

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?

avatar image calpolican · Feb 05, 2019 at 08:35 AM 1
Share

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.

Show more comments
avatar image
1

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image calpolican · Feb 05, 2019 at 08:38 AM 0
Share

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.

avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how do I make it so that my game only starts after the player presses Jump? 0 Answers

Time.timeScale doesn't stop void update() from beeing run 1 Answer

Pause button not "caught" by pause script 2 Answers

Time.timeScale=0 Problem Help please 1 Answer

Pausing an Invoke for a time 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges