x


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);

}
more ▼

asked Sep 08 '11 at 01:02 AM

zacwashere2011 gravatar image

zacwashere2011
11 10 10 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
    }
}
more ▼

answered Sep 08 '11 at 01:11 AM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

Thanks man! works well!

Sep 08 '11 at 01:13 AM zacwashere2011

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!

Apr 19 '12 at 07:12 AM bayerly
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x200

asked: Sep 08 '11 at 01:02 AM

Seen: 2302 times

Last Updated: Apr 19 '12 at 07:12 AM