simple timerfunction

in the code below i have a simple timer and it works like it should.
But it works only when i keep the key pressed. What I want is the function timer1 work one circle, if i press the key and release the key. How would i have to change the code to achieve this?

var timertime = 0.0;

function Update () { 

	if (Input.GetKey ("e")) {
  		timer1();
	}

	if (Input.GetKeyUp ("e")) {
 		resettimer();
	}
}


function timer1(){
	
	timertime += Time.deltaTime;
	
	if( timertime < 3){
		transform.Rotate(0,20 * Time.deltaTime,0);
	}
	
	else {
	transform.Rotate(0,0,0);
	}
}


function resettimer(){	
  	timertime = 0;
}

If you want it to keep going when you let go of the key get rid of the following bit:

if (Input.GetKeyUp ("e")) {
       resettimer();
}

But I think you want it so that when you release the key a second time it then resets, which you can do with the following:

var timertime = 0.0;
var second : boolean = false;

function Update () {

	if (Input.GetKey ("e")) {
		timer1();
	}

	if (Input.GetKeyUp ("e")) {
		if(second == true){
			resettimer();
			second = false;
		}
		else{
			second = true;
		}
	}
}


function timer1(){
   
	timertime += Time.deltaTime;
   
	if( timertime < 3){
		transform.Rotate(0,20 * Time.deltaTime,0);
	}
   
	else {
		transform.Rotate(0,0,0);
	}
}


function resettimer(){   
	timertime = 0;
}

This uses a boolean to check if it has already been pressed once.

hm, no thats not the result i want.
the logic i would like to get is:

  1. press and release a key
  2. rotate object for 3 seconds and stop rotating
  3. press and release a key
  4. rotate again for 3 seconds and stop rotating
    and so on …
var rotating = false;
function Update () { 
   if (Input.GetKeyUp ("e")  !rotating) { 
       rotating = true;
       Invoke ("StopRotation", 3.0);
   } 
   if (rotating) transform.Rotate(0,20 * Time.deltaTime,0);
} 
function StopRotation(){
	rotating = false;
}

Why didn’t the other way work?

@ Charles Hinshaw:

thank you very much, this is what i was trying to do , and one minute before your post i discovered the Invoke command but i didnt get it together yet.

@killer1390:

i cant tell you why, but it was the same problem with keeping the key pressed
even so thank you

Ya, I can’t really tell why the way you originally used didn’t work. But that is alright, if someone comes across this thread and reads this and know the answer PLEASE OH PLEASE, tell me.

I’ve added comments:

// The timer starts at 0
var timertime = 0.0; 

function Update () { 
   // This condition is only met when the "e" key is pressed down
   if (Input.GetKey ("e")) { 
        timer1(); 
   } 
   // This condition is only met when the "e" key is released
   if (Input.GetKeyUp ("e")) { 
       resettimer(); 
   } 
} 


function timer1(){ 
   // this is called every frame where the "e" key is held down
   // We are increasing the timer value by how long this frame is taking
   timertime += Time.deltaTime; 
   // So, e key is held down and has been held down for less than  3 seconds
   if( timertime < 3){ 
      // do our rotation
      transform.Rotate(0,20 * Time.deltaTime,0); 
   } 
   else {
   // this would mean that the "e" key has been held down for more than 3 seconds
   // but it doesn't really do anything. 
   transform.Rotate(0,0,0); 
   } 
} 

// this is called only on release, it just resets the timer
function resettimer(){    
     timertime = 0; 
}

So, going through this commented code, we get this behavior:

While “e” is pressed, the object will rotate as long as “e” has been pressed for less than 3 seconds. When “e” is released, the whole thing basically resets and the next press will start it over.

At least a cursory scan indicates that it “worked” in the sense that it did exactly what it should have done (what I described). That just isn’t what the author intended.

Thank you so much, I get it know. :smile:

My kingdom for “DoWhatIMeanNotWhatISay( );”

1 Like