Temporary invulnerability in a platformer

Hello,

I am working on a 2.5D platformer and we are in the finishing stages.

However, there is a feature I never understood how to implement (it's the first time I tried coding and I have zero programming formation).

We want to have a temporary invulnerability after the character takes a hit (like in the Mario games) so that life can't be drained in a second by continuous hits.

We are using "damage" trigger zones. How could I add a condition so that the trigger is only effective if there hasn't been damage in the last X seconds?

Here is the damage script:

var sound : AudioClip;

function OnTriggerEnter (other : Collider) { if(other.gameObject.tag == "Player"){ audio.PlayOneShot (sound); LifeGUI.charge--; } }

Many thanks in advance. Please remmember that I am not a programmer, so that what seems obvious to you isn't to me! ;)

You could use WaitForSeconds in conjunction with a boolean to create a timeout delay where damage cannot be applied. For example:

var sound : AudioClip;
var damageTimeout : float = 1.0;
private var canDamage : boolean = true;

function OnTriggerEnter (other : Collider)
{
   if(other.gameObject.tag == "Player")
   {
      if ( canDamage )    // if damage can be applied
         ApplyDamage();   // apply the damage
   }
}

function ApplyDamage ()
{
   audio.PlayOneShot (sound);            // play the sound
   LifeGUI.charge--;                     // subtract the life charge
   canDamage = false;                    // disallow further damage
   yield WaitForSeconds(damageTimeout);  // wait for the specified damage timeout
   canDamage = true;                     // allow damage again
}

Any questions, please ask.