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.
– AndyMartin458well, 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
– thenachotech1113AndyMartin458 & 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?
– kenny0630