- Home /
Open & Close a simple door animation with sound and door states
I'm trying to open a simple wooden door when my FPS player walks close to the door using a raycast and a taged door, I=My door opens fine with a nice creaky door sound but I can't seem to get it to close after I'm through the door. I'm trying to use a tmer but I'm sure I'm mucking it up somewhere although I get no script error mesages so I'm at a bit of a loss as to where I'm buggering it up??
Here is my script I'm using:
enum bobsDoorStates {open, closed}; var bobsDoorState : bobsDoorStates; var doorSound : AudioClip; var doorSoundClose : AudioClip; var timer : float = 0.0;
function Awake() { bobsDoorState = bobsDoorStates.closed; }
function Update () { }
function Open() { animation.Play("FBDoorOpen"); bobsDoorState = bobsDoorStates.open; audio.PlayOneShot (doorSound); }
if(timer >= 3){
Close();
}
function Close(){ animation.Play("FBDoorClose"); bobsDoorState = bobsDoorStates.closed; timer = 0; audio.PlayOneShot (doorSoundClose); }
Answer by Mike 3 · Mar 10, 2011 at 07:26 AM
I'd skip using your own timer and just do something like:
function Open() {
animation.Play("FBDoorOpen");
bobsDoorState = bobsDoorStates.open;
audio.PlayOneShot (doorSound);
Invoke("Close", 3);
}
Thanks $$anonymous$$ike, but I have a different sound for the door closing, how would I get that to work with this setup?
That's just the Open door function, the rest of the code stays as it was before ($$anonymous$$us the timer). The above just calls Close after 3 seconds
Thanks $$anonymous$$ike, I only now just had a chance to change my code and check it out, it works beutifully now. That was all I had to do was pull out the timer stuff and just add that once simple line of code you provided. AWESO$$anonymous$$E!! Thanks again man :)
You're welcome :D Also check out InvokeRepeating if you want to do something similar but called repeatedly
Your answer
Follow this Question
Related Questions
Need help with door opening and closing script/animation 3 Answers
Door Opening And Closing 2 Answers
how to make a door opening and closing. 1 Answer
Why is my door opening and closing at the same time????????? 2 Answers
open/close a animated door 1 Answer