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 :slight_smile:

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

You can call the EndCooldown() function directly from the animation. I think it’s a LOT easier in your case.