• 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 Jurikiin · Jun 27 at 12:03 AM · timescaledeltatime

Timer is incorrectly effected by Time.timeScale

Hello! I'm working on a timer for my game that's effected by Time.timeScale:

 float diff = Time.deltaTime * Time.timeScale;
 if (countDirection == CountDirection.Down) currentTime -= diff;
 else currentTime += diff;

Further, for testing, I have Input code for setting the timeScale to different values:

 if (Input.GetKeyUp(KeyCode.O)) Time.timeScale = 2f;
 else if (Input.GetKeyUp(KeyCode.P)) Time.timeScale = 0.5f;
 else if (Input.GetKeyUp(KeyCode.I)) Time.timeScale = 1f;

However, when Time.timeScale is either 2f or 0.5f, the timer is effected by a factor of 2.

ex. 2f = 4 times faster, 0.5f = 4 times slower.

I confirmed this with manual testing on my phone's stopwatch.

I'm not quite sure why this would be happening? Does anyone have any thoughts?

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
0
Best Answer

Answer by Jurikiin · Jun 27 at 02:13 AM

There must have been some code obfuscating what was happening.. subtracting Time.deltaTime is in fact affected by the timeScale. I can't pinpoint what was throwing that off, but after cleaning up my code it seems in fact nothing was needed after all..

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 Eno-Khaon · Jun 27 at 03:47 AM 0
Share

To help clarify your own discovery...

Time.deltaTime IS ALREADY affected by Time.timeScale. This means that you don't need to multiply it by timeScale, since that's already happened.

On a related note, this is also why Time.unscaledDeltaTime (and similar values) exists, for when you need an aspect to remain unscaled (for example, a pause menu fading in/out) while the gameplay itself is paused entirely.

avatar image
0

Answer by SkullerR · Jun 27 at 12:48 AM

The thing is that you are doing the Time.timeScale multiplication for every addition to the currentTime.


The thing that must be multiplied by the scale is your currentTime, not every addition or subtraction to it.


Remove the Time.timeScale; from

 float diff = Time.deltaTime * Time.timeScale;

then add something like this and use the value where u need it.

 float valueYouWantToUse = currentTime * Time.timeScale
Comment
Add comment · Show 4 · 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 Jurikiin · Jun 27 at 01:51 AM 0
Share

I'm still not sure I understand. In my example, if Time.timeScale = 1.1f, and Time.deltaTime = 0.1,

each frame, I want to subtract 1.1 from the time. In your case,

if start time is 10, then after 1 frame, currentTIme -= 0,1f

and then

currentTime *= TIme.timeScale would increase to infinity.

avatar image SkullerR · Jun 27 at 07:26 PM 0
Share

My mistake, I wrote this based on an older knowledge about Time.deltaTime. Nowdays it is already scaled by the Time.timeScale and Time.unscaledDeltaTime replaces the old one.

Just to clean up, you don't need to multiply by the scale.

avatar image Bunny83 SkullerR · Jun 28 at 02:18 AM 1
Share

Nowadays? deltaTime was always scaled by timeScale internally. It's true that unscaledDeltaTime was added later, but deltaTime always was affected by timeScale. See this old UA question for example.

avatar image SkullerR Bunny83 · Jun 28 at 03:33 PM 0
Share

nice, dunno why I assumed that too. I didn't gave much attention to my research. xD. Thanks for posting that, I will make sure I don't forget.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

140 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 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 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 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 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 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 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 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 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

How to smooth out movement without Time.deltaTime? 1 Answer

Single Step (pause, resume), or externally clock game loop 0 Answers

Timescale and Timers - what am I doing wrong? 1 Answer

TimeScale = 0 crashes Unity 1 Answer

Math.Abs slows down on time.timescale 0 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