Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Creat0r · Oct 24, 2013 at 01:22 PM · timeoafa

Time Slowdown

Hi. Now I have a problem with time slowdown. When I change Time.timeScale to 0.0f or to 1.0f, I have not problems. But, if I want to decrease it gradually, it doesn't work. I have tried several approaches. For example:

 //--------------some code--------------------
     if (pause)
     {
         while (Time.timeScale > 0.0f)
             {
               Time.timeScale = Time.timeScale - 0.1f;
               StartCoroutine(Slowdown());
             }
     }
     else {Time.timeScale = 1.0f;}
 
 //--------------some code--------------------
 
     IEnumerator Slowdown()
     {
        yield return new WaitForSeconds(0.1f);
     }

or this:

 //--------------some code--------------------
 if (pause) 
 {
   for (int i = 1;i<=10;i++)
   {  
     Invoke("Slowdown",0.1f);  
   }
 }
 else {Time.timeScale = 1.0f;}
 //--------------some code--------------------
 void Slowdown()
 {
   Time.timeScale -=  0.1f;    //42nd line      
 }

But they didn't bring a result. Last approach causes this error:

Time.timeScale is out of range. Needs to be between 0 and 100. UnityEngine.Time:set_timeScale(Single) Pause:Slowdown() (at Assets/Standard Assets/Scripts/Pause.cs:42)

I think timeScale drop below zero. But why? Help me, please.

Comment
Add comment · Show 4
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 sdgd · Oct 24, 2013 at 01:58 PM 0
Share

do you realize when you make time.scale 0 ot literally means Update () function is called 0 times / s

try getting code inside the OnGUI the event call

as OnGUI can be calles more than 10 times per frame or 1 per second.

so each time you change something for OnGUI it'll run through the code.

why it was giving you that error, ... because:

you called

 Invoke("Slowdown",0.1f);

several times

propably more than 10 times in 11 time it became -0.1f witch cannot be for time scale

avatar image robertbu · Oct 24, 2013 at 02:50 PM 0
Share

@sdgd - Changing Time.timeScale does not change the rate at which Update() is called. If your program is running at 60fps, then you will still get 60 Update() calls per second even when Time.timeScale is 0. What changes is Time.deltaTime.

avatar image Creat0r · Oct 24, 2013 at 05:14 PM 0
Share

sdgd, thank you and.

avatar image sdgd · Oct 25, 2013 at 09:32 AM 0
Share

@robertbu but afaik Time.deltaTime is how much time passed between 1 update and next update

2 Replies

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

Answer by robertbu · Oct 24, 2013 at 03:52 PM

There are a number of issues here. When Time.timeScale is set to 0.0, you do continue to get Update() calls, but Time.deltaTime will be 0.0. You will not get FixedUpdate() calls, and any WaitForSeconds() calls are scaled as well, so use of WaitForSeconds() in an IEnumerator will not work correctly. Time.time also stops. There is one value you can still use: Time.realtimeSinceStartup. Here is a coroutine that will scale time. If you want to go from 1.0 down to 0.0 in 3.0 seconds, you would call it:

 StartCoroutine(ScaleTime(1.0f, 0.0f, 3.0f);

You call this once, not repeatedly in Update().

 IEnumerator ScaleTime(float start, float end, float time) {
     float lastTime = Time.realtimeSinceStartup;
     float timer = 0.0f;
     
     while (timer < time) {
         Time.timeScale = Mathf.Lerp (start, end, timer / time);
         timer += (Time.realtimeSinceStartup - lastTime);
         lastTime = Time.realtimeSinceStartup;
         yield return null;
     }
     
     Time.timeScale = end;    
 }

 
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 Creat0r · Oct 24, 2013 at 05:07 PM 0
Share

Oh great! It works! Thanks a lot!

avatar image
0

Answer by mattssonon · Oct 24, 2013 at 03:01 PM

The while loop does not wait for the WaitForSeconds() function just because you call StartCoroutine(Slowdown())inside of it. Put the while loop inside of the IEnumerator instead, i.e. replace this:

 if (pause)
 {
     while (Time.timeScale > 0.0f)
     {
         Time.timeScale = Time.timeScale - 0.1f;
         StartCoroutine(Slowdown());
     }
 }

with

 if (pause)
 {
     StartCoroutine(Slowdown());
 }
 
 IEnumerator Slowdown() {
     while (Time.timeScale > 0.0f)
     {
         Time.timeScale = Time.timeScale - 0.1f;
         yield return new WaitForSeconds(0.1f);
     }    
 }

Also, change WaitForSeconds(0.1f); to something higher, waiting only 0.1 seconds is probably not going to look like a gradual change.

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 Creat0r · Oct 24, 2013 at 03:46 PM 0
Share

I've tried your approach. But it gives the same error again. First time slows, but than game (not time) stops. I really hope for your help.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Breathing sound effect using timers 1 Answer

Timer running down too quickly 1 Answer

How to create an Idle counter ? 2 Answers

Rewind? *confused* 4 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