- Home /
Make door play sound when you open and close it ?
Hai everyone. Today I need a bit of help to add more immersion to my game. I want a sound play when I open and close the door. Here's the script (not mine btw, thanks to Max A.V. https://www.youtube.com/watch?v=vfL7kQeZtic)
// 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);
}else{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
}
if(Input.GetKeyDown("e") && enter){
open = !open;
}
}
function OnGUI(){
GUI.color = Color.grey;
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press E to Interact");
}
}
//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;
}
}
I know you need to add 2 vars with SoundOpen and SoundClose, here's my try at it:
// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var soundOpened : AudioClip;
var soundClosed : AudioClip;
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);
var soundOpened : AudioClip;
}else{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
audio.clip = soundClosed;
}
audio.Play();
}
if(Input.GetKeyDown("e") && enter){
open = !open;
}
function OnGUI(){
GUI.color = Color.grey;
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press E to Interact");
}
}
//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;
}
}
But for some wierd reason, the sound plays randomly, and I can't open the door, whats the problem here ?
Answer by oliver-jones · May 12, 2014 at 04:11 PM
Its because your telling your AudioSource to play every Frame, which will cause a horrible sound. Try adding this within what you have already:
var playAudio : boolean = true;
//Main function
function Update (){
if(open){
//Open door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
var soundOpened : AudioClip;
}else{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
audio.clip = soundClosed;
}
if(playAudio){
audio.Play();
playAudio = false;
}
}
if(Input.GetKeyDown("e") && enter){
open = !open;
}
function OnGUI(){
GUI.color = Color.grey;
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press E to Interact");
}
}
//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
if(enter == false)
playAudio = true;
enter = true;
}
}
//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}
For some reason, the sound plays when I enter the trigger, not when I press "E" on the door, what could be causing it ? And I also can't open the door. I'll see if I can fix this, but it seems wierd.