- Home /
How could it be done "Cooldown" for a shield ?
Hi ,there! If that's not a lot to ask , i wanted to make that if i press the space button once then it needs to be disabled/blocked for like 22 seconds , because the power on and off animation duration is about that much time . So when the 22 seconds is over then it can be re-enabled so and it's start over again .
Here is the script so far :
#pragma strict
private var coolingDown;
function Update () {
if (Input.GetButtonDown ("Jump")){
SwitchTrigger();
EndCooldown();
}
if (!coolingDown)
{
coolingDown = true;
Invoke("EndCoolDown", 22.0f);
}
}
function EndCooldown()
{
coolingDown = false;
}
function SwitchTrigger() {
renderer.enabled = true;
collider.enabled = true;
animation.Play("Shield_On");
yield WaitForSeconds(10);
animation.Play("Shield_Low");
yield WaitForSeconds(2);
renderer.enabled = false;
collider.enabled = false;
}
I hope that everything is clear, and thanks in advance :)
Answer by Sonaten · Nov 06, 2012 at 01:19 PM
You have almost got it right I believe.
#pragma strict
private var coolingDown;
function start () {
coolingDown = false;
}
function Update () {
if (Input.GetButtonDown ("Jump") && coolingDown == false){
SwitchTrigger();
coolingDown = true;
Invoke("EndCoolDown", 22.0f);
}
}
function EndCooldown()
{
coolingDown = false;
ReversedSwitchTrigger()
}
function SwitchTrigger() {
renderer.enabled = true;
collider.enabled = true;
animation.Play("Shield_On");
yield WaitForSeconds(10);
animation.Play("Shield_Low");
yield WaitForSeconds(2);
renderer.enabled = false;
collider.enabled = false;
}
function ReversedSwitchTrigger() {
// code for the reversed actions of SwitchTrigger.
}
I also guess that you want to reverse the effects of the shield as it disappears.
Enjoy
Thanks for your help . But with this script it doesn't enable the mesh renderer or the collider for some reason .
Answer by astorga · Nov 06, 2012 at 01:26 PM
You can call the EndCooldown() function directly from the animation. I think it's a LOT easier in your case.
I think you need to elaborate :) (maybe It's something I can use)
Take a look at this link: http://docs.unity3d.com/Documentation/Components/animeditor-AnimationEvents.html