- Home /
Openable door by keypress with two different sounds
Hi
I have been searching for this a lot, didnt find a complete solution that I was able to get to work, started to puzzle scripts together wich was my first success of coding =) =P. My door opens and closes, it's a sound source with audio file attached, 2scripts on it; 1; door opens and closes on e-key press, 2; triggers sound source also on e key. But, i want 2 different sounds, one 4 open, one 4 closing. all on e-key press. So, there is Parent;emptygame obeject wich is trigger and has the two scripts, and there is the door mesh as child.
Here's the scripts: door open and close:
//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
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("e"))
{
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;
}
}
//@youtube.com/user/maksimum654321
//sound from door script:
var Trigger : AudioClip;
//when he enters the trigger zone
function OnTriggerStay()
{
//if he presses the e key
if (Input.GetKeyDown ("e"))
{
//audio plays
audio.Play();
}
}
Any1 know a solution ? perhaps it's neater with one script that does it all.
Thanks/ Joakim
Answer by Navij · Jan 14, 2013 at 11:15 AM
If i understood correctly, you need to add two public audioclip fields to your script:
var audio1 : AudioClip;
var audio2 : AudioClip;
and change this line
audio.Play(); //audio plays
to this:
if (open) audio.PlayOneShot(audio1);
else audio.PlayOneShot(audio2);
Thanks, But I can't get anything to work, I tried much. I think I still lack the basics of coding =P.
Navij, did u mean what u wrote fixing the "door open" script or for the sound script? I tried both, compiler errors or both sounds but at playstart only and then silence.
Navijs codefix + dooropenscript = comp.error . But here's the try: it complains about missing: open*
//Instruction: //$$anonymous$$ake an empty game object and call it "Door" //Rename your 3D door model to "Body" //Parent a "Body" object to "Door" //$$anonymous$$ake 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 //$$anonymous$$ake 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;
//$$anonymous$$ain 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.Get$$anonymous$$eyDown("e")){ open = !open; } } }
//Activate the $$anonymous$$ain function when player is near the door function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") { (enter) = true; } }
//Deactivate the $$anonymous$$ain function when player is go away from door function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") { (enter) = false; } }
//sound from door script:
var Trigger : AudioClip; var audio1 : AudioClip; var audio2 : AudioClip;
//when he enters the trigger zone function OnTriggerStay() { //if he presses the e key if (Input.Get$$anonymous$$eyDown ("e")) { if (open) audio.PlayOneShot(audio1); else audio.PlayOneShot(audio2); } }
//@youtube.com/user/maksimum654321
Answer by robertbu · Jan 14, 2013 at 06:23 AM
An easy way would be to just change the clip being played. The open script can set it to the open sound, the close script can set it to the close sound. The reference for AudioSource.clip has a bit of example source that swaps an audio clip.