- Home /
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;
}
}
Answer by ThePunisher · May 13, 2014 at 06:06 PM
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;
}
Basically, this will cause the open flag to alternate every time the player hits the "e" key.
Thanks for explaining. I added audio.Play after open line, now the sound plays corectly for opening and closing. Can't tell how happy I am, thanks again.
P.S: Is it possible to add delay for opening and closing ? For example I open the door, it waits for like 1 or 2 seconds and then I can close the door again.
Are you trying to simply add a delay? Or are you trying to wait on the door to finish opening/closing before you allow the player to open/close the door?
Yeah, I want so that when I open the door, I can't close it before it finishes opening.
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 deter$$anonymous$$ed 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!
Answer by mrlchristie · May 13, 2014 at 08:13 PM
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!