I am working on a simple open-able doors in Unity and I want them to have open and close AudioClips. I nearly got to where I want, but the only problem is that now, I hear the audio clips play immediately when I load or build the game scene, even thought I have Play On Awake unmarked, and I am far from the trigger collider. After wards everything goes perfectly and no problems. So I thought the problem might be somewhere in the script… but I have no idea where. Tested many examples I found here and there, but the problem remains.
If anyone had the same issue or might know whats causing it, I would really appreciate any help ![]()
Here’s the script:
@script RequireComponent(AudioSource)
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var audio1 : AudioSource;
var audio2 : AudioSource;
private var open : boolean;
private var enter : boolean;
private var defaultRot : Vector3;
private var openRot : Vector3;
function Start(){
var aSources = GetComponents(AudioSource);
audio1 = aSources[0];
audio2 = aSources[1];
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);
audio2.Play ();
}else{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
audio1.Play ();
}
if(Input.GetKeyDown("e") && enter){
open = !open;
}
}
//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;
}
}
EDIT: oh and one more thing. I have two AudioSources attached to the door body object with each audio clip.
And the sound also plays for just a second when I stop the game scene
if(open){ audio2.Play (); }else{ audio1.Play (); } so one sound is always set to play. Audio1 will always play as soon as you start the scene.
– AlucardJay