Door opens, plays sound, but can't close it.

Hey guys, I have a problem with my door. I found this script on youtube, and searched around how to make it play sound. I got it to where when I press E on it, the door opens and plays sound, but for some reason I can’t close the door when I open it. Here’s the script: (not mine btw)

// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
private var open : boolean;
private var enter : boolean;

private var defaultRot : Vector3;
private var openRot : Vector3;

function Start(){
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}

//Main function
function Update (){
if(open){
//Open door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
}else{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
}

if (Input.GetKeyDown("e") && enter) {
    if (!open) audio.Play();
    open = true;
}
}

function OnGUI(){
GUI.color = Color.grey;
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press E to Interact");
}
}

//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}

//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}

From what I can see in this script you currently have no way of closing the door. Try replacing the “e” key check with the following:

if (Input.GetKeyDown("e") && enter)
            {
                if (!open) audio.Play();
                open = !open;
            }

After the bit of code ThePunisher gave you I would then add another if statement to check to see how long the door has been open. For example you could set an “openTime” variable to 2 or 3 seconds (while your open animation plays) in the inspector and then use an if statement to check if those 2 or 3 seconds have passed by using something like

if(doorOpenTime > openTime){
ShutDoor();
doorOpenTime = 0.0f;
}

to then call a shut door function which will reverse your open animation maybe?

the doorOpenTime to check how long the door has been open can be determined by using:

doorOpenTime += Time.deltaTime;

and place it in the if statement written by ThePunisher above.

Hope this makes sense, and more importantly, I hope it would work!