Bullet time / Slow motion effect

Hi guys

I`m trying to add a bullet time effect to my game, but the problem is that it is constantly on when I hit the assigned key and that makes it way too easy.

I need a way of limiting the effect. It should only work for a certain time, and have a cool-down period before you can use it again.

Any ideas? And keep in mind that I`m new to scripting; my head is already raw trying to figure this yield stuff out.

My code so far:

function Update () {

if (Input.GetButtonDown ("Fire2")) {

    if (Time.timeScale == 1.0)

        Time.timeScale = 0.3;

    else

        Time.timeScale = 1.0;

        Time.fixedDeltaTime = 0.02 * Time.timeScale;

}

}

You can make a simple timer by checking how much time passes by adding deltaTime each frame. You tell bullet time to stop after so many seconds has passed.

var currentBulletTimer : float = 0;
var bulletTimeAllowed : float = 3.0; // seconds

function Update () {
    if (Input.GetButtonDown ("Fire2")) {
        if (Time.timeScale == 1.0)
            Time.timeScale = 0.3;
        else
            Time.timeScale = 1.0;
            
        //Time.fixedDeltaTime = 0.02 * Time.timeScale;
    }
    
    if (Time.timeScale == 0.3) {
    	currentBulletTimer += Time.deltaTime;      
    }      
        
    if ( currentBulletTimer > bulletTimeAllowed ) {
      	currentBulletTimer = 0;
       	Time.timeScale = 1.0;
    }
}

I just made this up on the spot so not sure if this is 100% accurate :/. Should work though.

var canSlowMo : Boolean = true;
    
    function Update () {
    
        if (Input.GetButtonDown ("Fire2") && canSlowMo) {
    
            Debug.Log("slooooow mooooo!");
    
            Time.timeScale = 0.3;
    
            canSlowMo = false;
    
            yield WaitForSeconds (10);
    
            Time.timeScale = 1;
            
            canSlowMo = true;
    
        }
    
        else{
    
            Debug.Log("Cannot enter slow mo yet!");
        }
    }