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 Dogg · Aug 02, 2014 at 02:10 AM · c#timepause

Pause Game When Function Is Enabled?

Okay I'm having a really big problem with this. I've been trying to pause the game when either a function is enabled, a collision with a certain gameobject happens, or when whenever. By the way I don't mean pause the game when all of these things happen, I mean I tried them all but it doesn't work for me. I just want to pause the game when I collect a power up, and wait for 3 seconds then un-pause the game. Time.timeScale never ever works for me by the way.

Look at this function:

 public void SpeedUp()
     {
         Debug.Log( "SpeedUp()" );
         Time.timeScale = 0;
 }

This is the beginning of the function as you can probably tell. I tried doing this and similar things but it won't pause anything. I also tried stopping time by collision, but I was told this wasn't going to work because the gameObject I'm colliding with destroys itself therefore destroying the Time.timeScale = 0;, which means it never happens. Here it is anyways if anyone knows how to fix this:

 public class PowerupScript : MonoBehaviour {
 
     
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player") {
             ControllerScript playerScript = other.gameObject.GetComponent<ControllerScript> (); 
             if (playerScript) {
                 
                 Time.timeScale = 0;
                 
                 // We speed up the player and then tell to stop after a few seconds
                 playerScript.SpeedUp();
                 
             }
 
                 Destroy (gameObject);
         }
     }
     
     
 }
 

I'm asking/begging you guys/gals. Please help.

Comment
Add comment · Show 3
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 Dogg · Aug 02, 2014 at 07:06 AM 0
Share

I may have created misunderstanding with what I posted, so let me clear that up. The SpeedUp function is not there to stop time, it is there to speed up my player when I collect a power up. The reason why I put a Time.timeScale there was to try and stop time before the speed up happens, and then after 3 seconds turn time back to normal and then I speed up. So again the SpeedUp is not for stopping time.

avatar image Dogg · Aug 02, 2014 at 10:35 PM 0
Share

Anyone else know of ways I can do this?

avatar image Dogg · Aug 06, 2014 at 07:15 AM 0
Share

Bump. I still need help.

3 Replies

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

Answer by Dogg · Aug 19, 2014 at 06:44 PM

Well I tried everything, nothing seemed to work out correctly for me. However, I managed to create a similar effect to stopping time, so I've solved my own problem. Thank you all though for even bothering to try and help me. I really appreciate it.

Comment
Add comment · 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
0

Answer by rutter · Aug 02, 2014 at 02:17 AM

Is the game slowing down? Is your debug call showing up in the console window?

If you want to "wait three seconds", bear in mind that most of Unity's time tracking -- including Time.deltaTime and Time.time -- is affected by timeScale. If you set timeScale to zero, all of those values will effectively stop ticking any time.

So, what can you do? You can use Time.realtimeSinceStartup, wait until that's gone up by three seconds, and then reset the time scale.

 float speedUpTime;

 void SlowDown() {
     speedUpTime = Time.realtimeSinceStartup + 3f; //3 seconds from now
     Time.timeScale = 0f;
 }

 void SpeedUp() {
     speedUpTime = 0f; //set back to zero; effectively ignored after that
     Time.timeScale = 1f;
 }

 void Update() {
     //are we supposed to speed back up?
     if (Time.realtimeSinceStartup > speedUpTime) {
         SpeedUp();
     }
 }
Comment
Add comment · Show 9 · 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 Dogg · Aug 02, 2014 at 02:25 AM 0
Share

I see then okay I'll try it out.

Okay so I tried it out, and the only one that the console shows working is the one in the void SpeedUp function. The void SlowDown is not working.

avatar image Dogg · Aug 02, 2014 at 02:26 AM 0
Share

Oh and by the way no my game does not slow down, nor does it debug anything.

And to answer you question in the code, no we are not supposed to speed it back up. If your talking about the Player that is. The only thing in my Update function is this:

 void Update()
     {
             if (timer > 5)        
                     timer -= Time.deltaTime;  
 
             if (grounded && Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
                     anim.SetBool ("Ground", false);
                         rigidbody2D.AddForce (new Vector2 (0, jumpForce));
             Audio$$anonymous$$anager.AudioInstance.PlaySound("Jump");
 }
avatar image Dogg · Aug 02, 2014 at 02:37 AM 0
Share

And one more thing. I should of showed you this before, so sorry. But here's the rest of my SpeedUp function:

 speed = defaultSpeed * coeffSpeedUp;
         Audio$$anonymous$$anager.AudioInstance.Play$$anonymous$$usic("SpeedUp"); // Calls the Audio$$anonymous$$anager and request for it to play "SpeedUp" audio clip.
 
         
         CancelInvoke ("EndSpeedUp"); // in case the method has already been invoked
         Invoke ("EndSpeedUp", coeffSpeedUpTime = 5 );
         Time.timeScale = 0;
avatar image rutter · Aug 02, 2014 at 03:09 AM 0
Share

If you set timeScale to zero, your Invoke call will never fire -- like I mentioned above, freezing time has certain effects that you need to account for.

avatar image Dogg · Aug 02, 2014 at 03:18 AM 0
Share

Yeah sorry about that I got rid of it just now. Anyways so far nothing's pausing. There's no slow down in time, nor is the console saying anything other than for both the void SpeedUp and the code in the Update. By the way, I had to stop the code in the Update function because it speeds me up right on start up.

Show more comments
avatar image
0

Answer by Mayank Ghanshala · Aug 07, 2014 at 08:40 AM

If you have done Time.timeScale = 0 , you will get time.deltaTime = 0, Here is my suggestion though it is not perfect. as in FixedUpdate it is render after each 0.02 sec I guess so you can add direct value to a float temp like temp+=0.02f and then you can check it if it is near your 2 sec and in that case you can do Time.timeScale =1;

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 Dogg · Aug 07, 2014 at 07:56 PM 0
Share

So let me make sure I understand you correctly. You want me to create a temporary float, put it in FixedUpdate with something like temp+=0.02f, and then check if it is near 2 seconds, finally if it is near 2 seconds turn Time.timeScale = 1;. Something like that right?

avatar image Mayank Ghanshala · Aug 18, 2014 at 05:22 AM 0
Share

ya something like that

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Fading To New Scene Problem? 1 Answer

Destroy Gameobject once 0 health 2 Answers

C# scriping help 0 Answers

Loading a new level from a previous one. 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