How to make trigger play animation only once?

I have a trigger that plays a door animation when i step on it but when i step out of the trigger and back in the animation plays again. I want it so that i go into the trigger and the door opens and stays open. Heres the script:

var dooropenandclose : AnimationClip; //this is your animation clip.
var Celldooropensound : AudioClip;
var door : GameObject; //this is your game object that you want to move on trigger.

function OnTriggerEnter(){

animation.Play ("dooropenandclose");
audio.PlayOneShot(Celldooropensound);

}

1 Answer

1

Use a boolean variable as a flag to indicate if the door is closed, and only open it if this flag is true:

var dooropenandclose : AnimationClip; //this is your animation clip.
var Celldooropensound : AudioClip;
var door : GameObject; //this is your game object that you want to move on trigger.
private var closed = true; // door initially closed

function OnTriggerEnter(){
    if (closed){ // only do it if door closed
        closed = false; // door now is open
        animation.Play ("dooropenandclose");
        audio.PlayOneShot(Celldooropensound);
    }
}

Yeah just wanted to say, thanks a bunch, this was uuber helpful in a general sense just to get a grasp on what can be done with private variables and basic if/thens. Feel like I"m "gettting" conditionals now!