- Home /
This question was
closed May 13, 2013 at 06:03 AM by
Fattie for the following reason:
Duplicate Question
This post has been wikified, any user with enough reputation can edit it.
Question by
CYVISUALEFFECTS · May 12, 2013 at 08:03 PM ·
javascriptdoorkey
Open doors with key & sound
I already have a script but I want to add sound to the opening door, and the ability to open the doors with a key. Here is the code:`enter code here: // 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("f") && enter){
open = !open;
}
}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
}
}
//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;
}
}
Comment
Best Answer
Answer by ExTheSea · May 12, 2013 at 08:06 PM
Here is a similar question just without the sound which i answered already:
To play sound check out this:
http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.html