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
0
Question by sovan · Oct 18, 2015 at 01:39 PM · time.timescale

Time.timeScale equal to zero does not freezes my scene.

Hello Friends,

In my survival game the health bar value of the player continuously reduces with time. In the pause menu I have used Time.timescale = 0 to pause the game and creating a panel. The problem is, the panel is created but the health bar value in the background still reduces. This is a serious drawback for my game. Please help me with this. I will attach the script as well as the health bar so that you get a clear view of what's happening.

Script: void Start () { PausePanel = GameObject.Find ("PanelForPause"); PausePanel.gameObject.SetActive (false); }

 public void GamePaused()
 {
     Time.timeScale = 0.0f;
     PausePanel.gameObject.SetActive (true);
 }
Comment
Add comment · Show 8
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 meat5000 ♦ · Oct 18, 2015 at 12:47 PM 0
Share

Read the timescale page of the scripting api. You missed something.

avatar image $$anonymous$$ · Oct 18, 2015 at 02:59 PM 0
Share

Is the function GamePaused() called every frame? That may be your problem. Try using a bool.

avatar image sovan $$anonymous$$ · Oct 18, 2015 at 03:06 PM 0
Share

No, it is called only if pause button is pressed. I haven't tried with bool.

avatar image meat5000 ♦ sovan · Oct 18, 2015 at 03:54 PM 0
Share

Didnt read it? Ill post it here in case you missed my comment.


The scale at which the time is passing. This can be used for slow motion effects.

When timeScale is 1.0 the time is passing as fast as realtime. When timeScale is 0.5 the time is passing 2x slower than realtime.

When timeScale is set to zero the game is basically paused if all your functions are frame rate independent.

Except for realtimeSinceStartup, timeScale affects all the time and delta time measuring variables of the Time class.

If you lower timeScale it is recommended to also lower Time.fixedDeltaTime by the same amount.

FixedUpdate functions will not be called when timeScale is set to zero.

Show more comments
Show more comments
avatar image sovan · Oct 18, 2015 at 07:36 PM 0
Share

using UnityEngine; using System.Collections;

public class PanelScript : $$anonymous$$onoBehaviour { public GameObject PausePanel; private GameObject SuccessPanel; bool isPaused = false; // Use this for initialization void Start () { PausePanel = GameObject.Find ("PanelForPause"); PausePanel.gameObject.SetActive (false);

// SuccessPanel = GameObject.Find ("PanelForSuccess"); // SuccessPanel.gameObject.SetActive (false); }

 public void GamePaused()
 {
     isPaused = true;
     PausePanel.gameObject.SetActive (true);
     Debug.Log ("Clicked Pause button");

 }

 public void GameResumed()
 {
     isPaused = false;
     PausePanel.gameObject.SetActive (false);
 }

 void Update()
 {
     if (isPaused == true)
         Time.timeScale = 0;
     else if (isPaused == false)
         Time.timeScale = 1;
 }

@meat5000, @Dusker Studios

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 18, 2015 at 05:36 PM

Like other have said setting Time.timeScale to 0 will do exactly those things:

  • Time.time will stop and not increase any more.

  • Time.deltaTime will return 0

  • FixedUpdate is no longer being called.

  • Everything based on Time.time or Time.deltaTime will also freeze (for example WaitForSeconds in a coroutine)

Everything else will keep running. So if something still move / changes, you most likely do it wrong. If you used Time.deltaTime whereever you change something over time, that process should stop since Time.deltaTime will return 0.

You haven't shown how your health bar is reduced. However if it still reduces when Time.timeScale is 0 you clearly doing it wrong.

Comment
Add comment · Show 5 · 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 sovan · Oct 18, 2015 at 05:41 PM 0
Share

public Scrollbar HealthBar; public float health = 100; private Animator animator; private GameObject Player;

 void Start () 
 {
     animator = GetComponent<Animator> ();
     Player = GameObject.Find ("player");

// PanelScript Script = gameObject.GetComponent(); // Script.Success$$anonymous$$essage (); }

 // Update is called once per frame
 void Update () 
 {
     health -= 0.030f;
     HealthBar.size = health/ 100f;

. . . . . .!

}alt text

life1.png (12.3 kB)
avatar image Bunny83 sovan · Oct 18, 2015 at 07:35 PM 0
Share

Well, as i thought. What you do here is simply wrong ^^. You are reducing your health by 0.03 every "frame". That way how fast your health is dropping depends on your frame rate.

At an assumed frame rate of 60 fps (quite usual with vsync on and a 60Hz monitor) you reduce your health by about 1.8 health per second. However if someone doesn't have vsync on and he gets something like 400fps then he would loose about 12 health a second. Likewise someone with a crappy PC that only get about 20 fps will live 3 times longer than the guy with 60fps or about 20 times longer than the 400 fps guy.

To keep things frame independent you should multiply your values by Time.deltaTime. That way you can directly work with "units" per second:

 health -= 2f * Time.deltaTime; // reduces by 2 hp per second

As mentioned Time.deltaTime will return 0 when timeScale is 0. So the line will still be executed but nothing will change.

avatar image meat5000 ♦ Bunny83 · Oct 18, 2015 at 07:40 PM 0
Share

Latest script he posted was added above to the comments.

avatar image AcidmanRPGz · Apr 19, 2020 at 11:37 AM 0
Share

Would you $$anonymous$$d telling me how I would freeze my AI and player $$anonymous$$ovement, since they are not based of Time.deltaTime? Also, I need the answer to this soon, since I’m doing the project requiring this for Ludlum Dare

avatar image jayghost1983 AcidmanRPGz · Apr 19, 2020 at 01:17 PM 0
Share

my multiplying their move rates by Time.deltatime...?

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

8 People are following this question.

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

Related Questions

Why is may costom yield not working? 2 Answers

Slow Mo effect with Time.timeScale? Dont understand Lerp and Corutines... 1 Answer

Changing Time.fixedDeltaTime to create slowmotion messes up with physics. 1 Answer

Slowing down time except for player. 0 Answers

create a pause state and menu that still allows animations 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