Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by andrewjm10 · Jun 30, 2013 at 09:53 PM · noobsupportaddon

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;
 }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
   }
 }
 }
 ...

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrewjm10 · Jun 30, 2013 at 10:16 PM 0
Share

It gives me the error, $$anonymous$$ Identifier: 'DoorOpenAngle'

avatar image Em3rgency · Jun 30, 2013 at 11:00 PM 1
Share

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.

avatar image andrewjm10 · Jun 30, 2013 at 11:37 PM 0
Share

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

avatar image Kiloblargh · Jul 01, 2013 at 12:54 AM 0
Share

Also, variable names should always start with a lowercase letter.

avatar image aldonaletto · Jul 01, 2013 at 03:08 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Enter Trigger, display Text, then delete object 1 Answer

Script outdated i think, Unity 4 - Compiler errors galore 1 Answer

What is wrong with my RockPaperSissors script? 1 Answer

Noob Question! Unexpected token: if. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges