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 coughin05 · Feb 21, 2015 at 09:53 PM · javascripttimetimerjavatime.timescale

Trouble with Time.Timescale

I'm trying to toggle time scaling using a GetKeyDown check. The trouble is that when I press the key, nothing happens. I have no errors in the console, and I've tested that the GetKeyDown and the if statements are working by adding a print event, which works fine. I'm also moving the object along its Y axis using transform.Translate, just to see whether timescaling is working.

Here's the code:

 function Update ()
 {
 transform.Translate (0, 0.02, 0);
 if ( Input.GetKeyDown ( "s" ) )
     {
         if ( Time.timeScale == 1.0 )
         {
             Time.timeScale = 0.2;  //slow time down
             print ( "Time slowed." );
         }
         else
         {
             Time.timeScale = 1.0;  //regular speed
             print ( "Time Restored." );
         }
 }



Like I said, no errors, and the printing is working just fine. So what's the problem here?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by tanoshimi · Feb 21, 2015 at 09:58 PM

Your movement is not framerate-independent, so it won't be affected by Time.timeScale. Update() always runs as often as it can - irrespective of timeScale - and you're moving your transform by 0.02 units every time it is called.

Your translate should be made frame rate independent as follows:

 transform.Translate (0, 0.02 * Time.deltaTime, 0);

(You might also need to scale the 0.02 up to around 1 instead)

For more info: http://answers.unity3d.com/questions/270116/timetimescale-seems-doesnt-work-with-update.html

Comment
Add comment · Show 2 · 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 LividPixel · Feb 22, 2015 at 12:37 AM 0
Share

Aye. Just multiplying by Time.deltaTime will make the movement be 0.02 units per second, which is a lot slower than 0.02 units per frame (which it was previously.)

avatar image coughin05 · Feb 22, 2015 at 01:07 AM 0
Share

Ohhh, brilliant. What a detailed explanation. Thanks for taking the time to answer. Cleared up more than one thing for me.

avatar image
0

Answer by vintar · Feb 21, 2015 at 09:57 PM

Basically your script is saying if time scale != 1 then set it to 1 :) you should set the scale to 0.2 on key down and set it back to 1 on key up.

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 LividPixel · Feb 22, 2015 at 12:36 AM 0
Share

As that code is only being called on frames in which the 's' key was just pressed, I believe it should be functioning as a toggle perfectly fine.

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

Timer help please 1 Answer

How do I stop 'timeSinceLevelLoad' without pausing entire game? 2 Answers

Pause & Unpause Different timers 1 Answer

How to change overlay with timer? 0 Answers

Random spawn timer 1 Answer


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