- Home /
The question is answered, right answer was accepted
How to add a sound effect on a Door?
I am pretty new to Unity and I am doing a project on making a character to move around. I have successfully move the character around by using Character Controller and manage to open and close the door by pressing the F and G keys respectively.
The problem now I faced is that I can't seem to put a open and close door sound effect to the door... I have the following javascripts:
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;
var DoorOpen : AudioClip;
var DoorClose : 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("f"))
{
(open) = true;
}
else if(Input.GetKeyDown("g"))
{
(open) = false;
}
}
}
//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 away from the door
function OnTriggerExit (other : Collider)
{
if(other.gameObject.tag == "Player")
{
(enter) = false;
}
}
var Trigger : AudioClip;
//When player enters the trigger zone
function OnTriggerStay()
{
if(Input.GetKeyDown("f"))
{
if (open) audio.PlayOneShot(DoorOpen);
else audio.PlayOneShot(DoorClose);
}
}
The sound is just not playing or anything...Please do help me.. Thank you very much!!
Try putting a debug log inside of the on trigger stay function to make sure that those calls to audio are being made.
well, i have not used javascript in a while, but there could be many things going on, including a bug. so just try to use debug.log() to find out where the problem is
Andy$$anonymous$$artin458 & thenachotech1113: I tried to put the debug.log(); into the OnTriggerStay function but it says unknown identifier..... or is there any other way to add the sound?
Answer by kenny0630 · Nov 13, 2013 at 04:18 PM
Problem solved.... it happened to be a misplace of the Audio Source... It should be placed under the parent not the child Door... and I've changed the scripts of the OnTriggerStay function slightly:
function OnTriggerStay()
{
if(Input.GetKeyDown("f"))
{
audio.PlayOneShot(DoorOpen);
}
if(Input.GetKeyDown("g"))
{
audio.PlayOneShot(DoorClose);
}
}
and now it works perfectly fine with opening and closing the door~ :) Thank you for the help guys~
Nice you made it, still consider what I am saying in my answer. It works, but it may be that it will work most of the time but not all the time.
Answer by fafase · Nov 13, 2013 at 04:20 PM
It is not a good idea to mix Input and Physics.
OnTriggerStay is done by the physics engine while Input comes fro your OS.
Both are not running the same speed. GetButtonDown returns true only on the frame you are pressing, if the physics is not run on that frame, nothing happens.
I would suggest to modify your code, so that the Input is taken care in the Update and the Trigger is using Enter and Exit to set a boolean on and off.
var isIn:boolean = false;
function OnTriggerEnter()
{
isIn = true;
}
function OnTriggerExit()
{
isIn = false;
}
function Update()
{
if(Input.GetKeyDown("f") && open && isIn){
audio.PlayOneShot(DoorOpen);
}
}
thank you for your help! I have manage to swap the codes!! if(enter == true) { if(Input.Get$$anonymous$$eyDown("f")) { (open) = true; audio.PlayOneShot(DoorOpen); } else if(Input.Get$$anonymous$$eyDown("g")) { (open) = false; audio.PlayOneShot(DoorClose); } }
I've placed it in the update function.