- Home /
Open Door Once But Sound Plays again when i trigger at the door
Hey Everybody
in my scene i have a door that you can open by pressing F then it also plays a sound from the doors Audio Source, but i need the audio to only play one time, since the door cannot be closed again,
So when the door is open and i press F at the open door it stills plays the audio
Can you help me fix script so it doesnt ?
THanks in advance
Here's my script
// 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); }
if(Input.GetKeyDown("f") && enter){ open = !open; audio.Play(); } }
//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; } }
Please format your code by highlighting it and clicking the little button with the 1s and 0s in the editor.
Answer by Sickleadz · Jan 25, 2014 at 11:53 PM
Thanks for the fast answer
i changed the code to what your wrote but the sound still plays, when door is open and i press f
hmmm, weird..
When you respond to someone use comments. Also format your code so people can read it. Watch the unity answers tutorial video. It's always at the right of the page.
Okay, I've edited my answer slightly. You had it so that open
toggled between true and false when you pressed F, now it only changes to true.
Answer by oatsbarley · Jan 25, 2014 at 11:05 PM
Where you have audio.Play()
you need to check whether the door is open already:
if (Input.GetKeyDown("f") && enter) {
if (!open) audio.Play();
open = true;
}
That should only play the audio when the door is closed.
Answer by superluigi · Jan 26, 2014 at 01:02 AM
The main problem is here
function Update ()
{
if(open)
{
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
}
if(Input.GetKeyDown("f") && enter && !open)
{
open = true;
audio.Play();
}
}
By adding open to the if condition now you can only do it once. When you press f and enter and the doors open = false then play the audio and open becomes true. So now that open = true it no longer meets the if statements conditions (input F key(yes), enter(yes), !open(no)) get it?
If you need help understanding my answer let me know and I will explain it. As it is though it should work.
As far as I can make out, open
being true does mean the door is open in @sickleadz code. Although it's not exactly clear and I could be misreading it!
Yea it confused me at first. I was gonna change it for him but I left it alone. I think I'm gonna rewrite it for him and just have him copy and paste.
Your answer
Follow this Question
Related Questions
Make door play sound when you open and close it ? 1 Answer
Audio issue - Trying Waitforseconds 0 Answers
Door opens, plays sound, but can't close it. 2 Answers
Play sound ONCE! 3 Answers
Can not play a disabled audio source persistent problem 0 Answers