Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Internetman · Mar 15, 2016 at 11:34 AM · c#texttimertimescaleaffect

Timescale not affect timer? (C#)

Hello my fellow Unities! I'm making a 2D mobile survival game, where I have a timer counting upwards that is printed on the screen. I also have a power up, that is used to slow down the enemies and everything around them. Now I have a problem, is it possible to make the timer ignore the Time.Timescale while the power up is in use?

How would I go forward in solving this?

Here's the code for the timer:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TimerScript : MonoBehaviour 
 {
     //Create timer
     public static float timer = 0f;
 
     //Create displayed timer
     Text text;
     
     void Awake()
     {
 
         //Load Text class
         text = GetComponent<Text> ();
 
         //Reset the timer
         timer = 0f;
 
     }
 
     void Update()
     {
         //Start the timer and keep it going
         timer += Time.deltaTime;
 
         //Write out the timer on the screen
         text.text = "" + timer.ToString("F1");
     }
 
 }

And here's the code for the power up and when it's in use:

 void OnTriggerEnter2D(Collider2D col)
     {
         if(col.gameObject.tag == "PowerUp1")
         {
             if(Time.timeScale == 1.0f)
             {
                 Time.timeScale = 0.2f;
                 Destroy(GameObject.FindGameObjectWithTag("PowerUp1"));
             }
         }
 }
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
3
Best Answer

Answer by phil_me_up · Mar 15, 2016 at 11:39 AM

You could always just compensate by multiplying Time.deltaTime by the same factor you changed the timescale by.

 timer += Time.deltaTime * (1.0f / 0.2f);

You changed your timescale to be 1/5th of the original time, so compensate by counting up 5 times faster.

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 Internetman · Mar 16, 2016 at 08:35 PM 0
Share

This worked for me! Thank you my friend!

avatar image
5

Answer by Sscia · Mar 15, 2016 at 11:52 AM

There is one property not affected by TimeScale which is Time.unscaledTime, it will return the time passed since the beginning of the game unaffected by the timescale changes.

This solution will require you to alter your algorithm a bit: instead of adding deltaTime to your counter every frame, save Time.unscaledTime when the timer starts running (something like _timerStartTime), and calculate the timer every frame by reducing _timerStartTime from the current Unscaled Time (giving your the current timer)

Here's some code, but it is not machine tested:

 void ResetTimer()
 {
     _timerStartTime = Time.unscaledTime;
 }

 void Update()
 {
     ...
     timer = Time.unscaledTime - _timerStartTime
 }
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 Bunny83 · Mar 17, 2016 at 05:05 AM 1
Share

Uhm you can also use Time.unscaledDeltaTime which would be exactly what the OP wants ^^. It's the current deltaTime without any scaling.

Note: As far as i know the unscaledDeltaTime value also has no clamping. So for example if the loading of a scene took very long (for example 3 sec), then Time.unscaledDeltaTime would be 3.0f the next frame. Time.deltaTime is clamped to 0.333 by default to prevent strange effects.

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

110 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

Related Questions

Distribute terrain in zones 3 Answers

Help with making a triggered gui message 1 Answer

How do I make the timeScale not affect the timer? 1 Answer

Multiple Cars not working 1 Answer

How to access a text file once it's on Android Device so I can read it. 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