Power Up Script Problem!

I am making a simple jumping game and I have made a sphere which is going to be my power up. I want it so when I collide with the sphere, it sets the Time.timeScale to 0.5 for only 10 seconds. Then after the 10 seconds are up, it sets the Time.timeScale back to 1.

This is my current script:

function OnTriggerEnter ()
{
 Destroy(gameObject);
Time.timeScale = 0.5;
}

Right now when I collide with it, it destroys the power up and set the Time.timeScale to 0.5, but I can’t figure out how to do this for only 10 seconds?

Any help or scripts appreciated!

1 Answer

1

function OnTriggerEnter ()
{

   Time.timeScale = 0.5;
    yield WaitForSeconds(10 * Time.timeScale);
    Time.timeScale = 1;
    Destroy(gameObject);
}

Thanks so much! Will try the script out!

Tried out script but does exactly what my old script used to do. It doesn't turn the time scale back to 1 after 10 seconds. HELP!

Updated the answer, shouldn't have been destroying the object (because it stops the coroutine) until after the time out.

If you need it to be invisible for the time consider just moving it a long way at the start...

Thanks, I'll try it!