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