- Home /
Door Opens When Not In Trigger And Sound Help
I made a door and scripted it so whenever I hit "e" it opens or closes, however whenever i hit "e" it opens or closes regardless of where i am in the map. only when i step in the trigger at least once does it make it so i cant.
This is the script for the door; // Smothly open a door var smooth = 2.0; var DoorOpenAngle = 90.0; var DoorCloseAngle = 0.0; var open : boolean; var enter : boolean;
//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("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
I am also wondering what to do to make a sound play whenever the door opens or closes as well.
Answer by sparkzbarca · Oct 15, 2012 at 02:01 AM
you have the bool enter.
It appears that determines whether your allowed to open the door right?
but when you declare a bool its default value is true;
so
bool enter;
if (enter)
{
will happen cause its true by default
}
the answer is to go to where you declare true or the function OnStart() and set enter to false.
you want to look at audiosource class to add sound
if you have a game object and you'd like to play on open add a audiosource component to the object then call play
for example
GetComponent().Play();
since your newish just go into the editor select the door and
Component menu up top Audio Audio Source
now drag and drop in a sound to play. just find one on your comp to test with.
DISABLE play on awake, that will play the sound at the game start
now the code that actually opens the door just before it.
GetComponent().Play();
pretty sure the default value for an uninitialized boolean is actually FALSE
but yeah, make sure it's not checked in the inspector
Your answer
Follow this Question
Related Questions
Sound on collision not working 1 Answer
Trigger door animation with external trigger 1 Answer
Why my sound it's not attaching to my Flashlight 2 Answers
Play Sound on trigger 2 Answers
Play sound on trigger, sound is coming from the trigger 1 Answer