How can I add the doors sound opening and closing to this script?
So I would really like to add sound to my door when it opens and closes (two sounds) but I don't know how to
This is the script that I'm dealing with:
pragma strict
private var guiShow : boolean = false;
private var enter : boolean;
var isOpen : boolean = false;
var door : GameObject;
var rayLength = 10;
function Update() { var hit : RaycastHit; var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "Door")
{
guiShow = true;
if(Input.GetKeyDown("e") && isOpen == false)
{
door.GetComponent.<Animation>().Play("open");
isOpen = true;
guiShow = false;
}
else if(Input.GetKeyDown("e") && isOpen == true)
{
door.GetComponent.<Animation>().Play("close");
isOpen = false;
guiShow = false;
}
}
}
else
{
guiShow = false;
}
}
function OnGUI(){ if(guiShow == true) GUI.Label(new Rect(Screen.width/1.9 - 75, Screen.height - 100, 150, 30), "Use door"); }
Answer by Vice_Versa · Aug 27, 2015 at 07:52 AM
add an audio source component to your object, in your script, make a public AudioClip object, then use AudioSource.PlayClipAtPoint(). heres a link to the documentation http://docs.unity3d.com/ScriptReference/AudioSource.PlayClipAtPoint.html