- Home /
This question was
closed Apr 14, 2013 at 07:30 AM by
Fattie for the following reason:
Duplicate Question
Question by
$$anonymous$$ · Apr 14, 2013 at 06:03 AM ·
triggerdoor
How To Add A Sound Effect To A Door?
Hey all you awesome peeps! Can maybe one of you awesome peeps kindly show me how to add a sound effect when opening or closing a door. Here is the script for the door. BTW it uses a trigger.
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){
open = !open;
}
}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'E' to open the door");
}
}
//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;
}
}
Comment
in general add a line like this
var heroRunFinished:AudioSource;
hit save and LOO$$anonymous$$ in your editor -> inspector. drag a sound in to there.
in your code do this
heroRunFinished.Play();
probably at line 38, but it depends on many things and is a general program$$anonymous$$g question.
there are 100s of answers on this, on here, next time just search
pls "tick" any useful answer to close out the question