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

1 Answer

1

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 Mike, 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 (minus the timer). The above just calls Close after 3 seconds

Thanks Mike, 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. AWESOME!! Thanks again man :)

You're welcome :D Also check out InvokeRepeating if you want to do something similar but called repeatedly