- Home /
Only access door if it's not locked?
i am using a script i found, it works just fine i added sound affect to it except i would like to add a function to allow some doors to be locked.
Something like: var IsDoorLocked = 0; // 1= locked 0 = unlocked
set it on each door and display a message "Door is Locked!" and cancle the doop opening, just wondering if there is a simple way to do this.
 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("e") && enter){
 open = !open;
 if (!audio.isPlaying){
     audio.Play();
   }
 }
 }
 
 function OnGUI(){
 if(enter){
 GUI.Label(new Rect(Screen.width/17 - 50, Screen.height - 200, 250, 80), "Press (E) To Interact");
 }
 }
 
 //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;
 }
 }
Answer by aldonaletto · Jun 30, 2013 at 10:07 PM
You could just add a boolean variable locked and verify it in Update and OnGUI:
 // declare the locked variable public, so that other scripts can modify it
 public var locked : boolean; 
 
 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);
 }
 // only verify key and enter if not locked:
 if(!locked && Input.GetKeyDown("e") && enter){
 open = !open;
 if (!audio.isPlaying){
     audio.Play();
   }
 }
 }
  
 function OnGUI(){
 var r = new Rect(Screen.width/17 - 50, Screen.height - 200, 250, 80);
 if (enter){
   if (locked){ // if locked, display warning
     GUI.Label(r, "Door locked!");
   } else { // not locked: display instruction
     GUI.Label(r, "Press (E) To Interact");
   }
 }
 }
 ...
It gives me the error, $$anonymous$$ Identifier: 'DoorOpenAngle'
Your own code has DoorOpenAngle in line 9, but you're not declaring it anywhere or assigning it (at least not in the code we can see). That's why you get the error. Unity has no idea what 'DoorOpenAngle' is.
Yeah lolo i forgot to add the var DoorOpenAngle = 90.0; I see an issue though it no longer detects my player? No message is displayed now
Also, variable names should always start with a lowercase letter.
andrew, have you replaced your original script with the code above? This code isn't complete: it contains only the modified parts. If this is the problem, copy the OnTrigger events from your question and paste them at the end of the script.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                