- Home /
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.
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.
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.
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();
}
}
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.
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");
}
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;
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.
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.
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;
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?
Your answer
Follow this Question
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