i had open/close door script,how to add sound?

how to add open /close door sound when i press key f

//Instruction:
//Make an empty game object and call it "Door"
//Rename your 3D door model to "Body"
//Parent a "Body" object to "Door"
//Make sure thet a "Door" object is in left down corner of "Body" object. The place where a Door Hinge need be
//Add a box collider to "Door" object and make it much bigger then the "Body" model, mark it trigger
//Assign this script to a "Door" game object that have box collider with trigger enabled
//Press "f" to open the door and "g" to close the door
//Make sure the main character is tagged "player"

// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;

//Main function
function Update (){

if(open == true){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
Time.deltaTime * smooth);
}

if(open == false){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
Time.deltaTime * smooth);
}

if(enter == true){
if(Input.GetKeyDown("f")){
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;
}
}

You could add an AudioSource (Component->AudioSource) to the object this script is attached.

var opening:AudioClip;   // drag the sounds in the Inspector
var closing:AudioClip;
var isIn:boolean;

function Start(){
   isIn = false;
   audio.clip= opening;
}
function Update(){
   if(isIn){
       Debug.Log("Player is in the zone");
       if(Input.GetKeyDown("f")){
         open = !open;
         Debug.Log("Player is pressing and open is: "+open);
         if(open){
           Debug.Log("Audio is playing or I read this but no sound, I have not built up my sound properly, no point I get back to the forum to ask, the problem is all mine now");
           audio.clip = opening;
           audio.Play();
         }
         else{
           Debug.Log("Audio is playing or I read this but no sound, I have not built up my sound properly, no point I get back to the forum to ask, the problem is all mine now");
           audio.clip = closing;
           audio.Play();
         }
      }
   }
}

function OnTriggerEnter(other:Collider){
  if(other.gameObject.tag=="Player"){
    isIn= true;
    }
}
function OnTriggerExit(other:Collider){
  if(other.gameObject.tag=="Player"){
    isIn=false;
    }
}

Make sure to tick off PlayOnAwake and Loop in the AudioSource component.

I guess that should do it. Try it and report.

EDIT: I added some debugs to understand where it is wrong and reinstall the whole thing

Please help, anyone can put a code that works.

I’ve combined these codes together to get this to work. Works great now! :slight_smile:
(make sure you drop the sound in the inspector)

//Instruction:
//Make an empty game object and call it "Door"
//Rename your 3D door model to "Body"
//Parent a "Body" object to "Door"
//Make sure thet a "Door" object is in left down corner of "Body" object. The place where a Door Hinge need be
//Add a box collider to "Door" object and make it much bigger then the "Body" model, mark it trigger
//Assign this script to a "Door" game object that have box collider with trigger enabled
//Press "f" to open the door and "g" to close the door
//Make sure the main character is tagged "Player"
 
// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;
var opening:AudioClip;
 
//Main function
function Update (){
 
if(open == true){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
Time.deltaTime * smooth);
}
 
if(open == false){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
Time.deltaTime * smooth);
}
 
if(enter == true){
if(Input.GetKeyDown("y")){
open = !open;
if(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;
}
}

Hyperactive, your script causes the door to change the angle for some reason. It was upright, but on play, it’s horizontal in the middle of the doorway:

The rotation xyz are constantly updating in play mode too.

Place a sound after the door open or close is easy! The hard part is creating a scrpit with the door opening and closing. You asked: "how to add sound?
Do this:

    using UnityEngine;

    using System.Collections;

    public class AudioDoor : MonoBehaviour {

public AudioClip openAudio;

public AudioClip closeAudio;

	void Update () {

		if(//YOUR CONDITION OPEN DOOR//){
		audio.PlayOneShot(openAudio);
		}

		if(//YOUR CONDITIONCLOSE DOOR//){
			audio.PlayOneShot(closeAudio);
		}

    }