Restart level when there is no input for some time

Hi,

i know this is really basic question, but i really cant find any solution anywhere.

Thanks.

3 Answers

3

You can check for a key down and put a timer in it:

private float timer = 0;

void Update() {
        if (Input.anyKeyDown)
            resetTimer();
        else
            tick();
    }

void resetTimer(){
        timer = 0;
    }

void tick(){
        timer += Time.deltaTime;
    }

when your timer is bigger then a value you like, for example one minute, you can check it in Update, if( timer > 60) { restartlevel } etc.

thanks. there is problem with float, it says it is unexpected token. I also hope that i added the function update correctly. so where is mistake? private float timer = 0; void Update() { if (Input.anyKeyDown) resetTimer(); else tick(); } void resetTimer(){ timer = 0; } void tick(){ timer += Time.deltaTime; } function Update () { if( timer > 10) Application.LoadLevel("futura"); }

C# or UnityScript? You have a bit of both in there.

oh, maybe that is the problem, i am trying to write it in javascript....

Oh yeah, did it by myself. thanks everyone for help. here is final code:

var timer : float = 0;
 
function Update() {
        if (Input.anyKeyDown)
            resetTimer();
        else
            tick();
if( timer > 10) 
        Application.LoadLevel("futura");
 
    }
 
function resetTimer(){
        timer = 0;
    }
 
function tick(){
        timer += Time.deltaTime;
    }

Record the time of your last input. Subtract it from the current time. If the result is higher then a threshold then reload the level