Hello. This is my code for a star fox style shield (the button is pressed, the shield object is active for a second, and then it is not active). If I press q that happens, the shield become active and then after a second, it deactivates. The problem is that I cannot do it again, when I press q nothing happens. What is wrong? Thanks
function Update(){
if( Input.GetKeyDown( "q")){Invoke("ShieldON",0);} else {Invoke("ShieldOFF",1);}
}
function ShieldON (){
SHIELD.active=true;
}
function ShieldOFF(){
SHIELD.active=false;
}
You might be able to figure it out from there. Sorry I couln't help further. I had this problem as well, and I got a solve, and I believe it was like this, but it still doesn't work =/. Good luck none-the-less!
Your problem is that you invoke ShieldOFF every frame (every invoke get delayed 1 sec but after that sec ShieldOFF is called ... every frame)
Input.GetKeyDown() is only true for one frame (because it just tells you the event "Keydown")
that means in the next frame ShieldOFF is called again.
The second script that Justin Warner posted above should work.
You can change your script to:
function Update(){
if( Input.GetKeyDown( "q")){
ShieldON();
Invoke("ShieldOFF",1);
}
}
function ShieldON (){
SHIELD.active=true;
}
function ShieldOFF(){
SHIELD.active=false;
}
ShieldON don't need to be invoked that way when you don't want a delay. Just call the function.
Now when you press "q" ShieldON is called instantly and turn your shield on. But also Invoke ShieldOFF with 1 sec. delay.
After that sec. ShieldOFF should be called.