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 Tronicx · Jul 01, 2014 at 11:46 AM ·

Making opening door requires a Key

Greetings, I'm trying to make a door that opens only if the player has the key I made the door and the key and the inventory script, and i have been facing problems with the door Script, Here is the code i made so far but it is not working ( I'm new at all this so sorry if you find my code is a big mess ) var smooth = 2.0; var DoorOpenAngle = -89.0;

 private var open : boolean;
 private var enter : boolean;
 private var defaultRot : Vector3;
 private var openRot : Vector3;
 private var playerInventory : PlayerInventory;  
 
 public var requireKey : boolean; 
 public var doorSwishClip : AudioClip;               
 public var accessDeniedClip : AudioClip;            
 
 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;
 }
 }
 
 function OnGUI(){
 if(enter){
 GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 200, 150, 30), " ");
 }
 }
 
 
 function OnTriggerEnter (other : Collider){
 
 if (other.gameObject.tag == "Player") 
 {
    if(playerInventory.hasKey)
      {   
     enter = true;
      }
         else
         {
           audio.clip = accessDeniedClip;
           audio.Play();
          }       
 }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by meakeel · Jul 01, 2014 at 12:15 PM

I would personally use the trigger events to tell when a player is near the door then you can just call the open or close function.

If you add Debug.Log("Some Text Here"); then while playing the game you can see the text appear in the console when an event is triggered

Can you tell what part of the code isn't working?

     private var open : boolean;
     private var enter : boolean;
     private var defaultRot : Vector3;
     private var openRot : Vector3;
     private var playerInventory : PlayerInventory;  
      
     public var requireKey : boolean; 
     public var doorSwishClip : AudioClip;               
     public var accessDeniedClip : AudioClip;            
      
     function Start(){
     defaultRot = transform.eulerAngles;
     openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
     }
      
     //Main function
     function Update (){}
      
     function OnGUI(){
     if(enter){
     GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 200, 150, 30), " ");
     }
     }
      
     function OnTriggerEnter (other : Collider){
 
     Debug.Log("Door Collider Triggered");
 
     if (other.gameObject.tag == "Player") 
     {
        if(playerInventory.hasKey)
          {   
         open();
          }
             else
             {
               audio.clip = accessDeniedClip;
               audio.Play();
              }       
     }
     }
     
     public void open ()
     {
     //Open door
     transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
     }
     
     public void close ()
     {
     //Close door
     transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
     }
 

Comment
Add comment · Show 4 · 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 Tronicx · Jul 01, 2014 at 06:38 PM -1
Share

$$anonymous$$y code is in JS, so the "public void open" and "public void close" dose not work at start

avatar image meakeel · Jul 01, 2014 at 10:05 PM 0
Share

Dam sorry, I'll post a js version in the morning for you.

Should just be a case of switching "public void" for "function"

avatar image meakeel · Jul 02, 2014 at 10:59 AM 0
Share

Hi Tronicx, Please find the code below, let me know how you get on with it

     private var open : boolean;
     private var enter : boolean;
     private var defaultRot : Vector3;
     private var openRot : Vector3;
     public var playerInventory : boolean;  
  
     public var require$$anonymous$$ey : boolean; 
     public var doorSwishClip : AudioClip;               
     public var accessDeniedClip : AudioClip;      
     public var smooth : int;
     public var DoorOpenAngle: int;    
     public var Door_Position: Transform;   
 
 function Start () {    
 
 smooth = 2;
 DoorOpenAngle = -89;
 Door_Position = GameObject.FindWithTag("door1").transform;
 playerInventory = true;
 defaultRot = transform.eulerAngles;
 openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
 
 }
 
 function Update () {
 
 }
 
 
 function OnTriggerStay (other : Collider){
  
     Debug.Log("Door Collider Triggered");
     
     if (other.gameObject.tag == "Player") 
     {
     yield $$anonymous$$eyCheck();
      }
     }
     
     
 function OnTriggerExit (other : Collider){
  
     //Close the door when the player leaves the door collider if the door is open
  
     if (other.gameObject.tag == "Player" && open) 
     {
     close_door();
     }
     }
     
     
     function open_door ()
     {
 //    Open door
     Door_Position.eulerAngles = openRot;
     open = true;
     }
  
     function close_door ()
     {
     //Close door
     Door_Position.eulerAngles = defaultRot;
     open = false;
     }
     
     
     function $$anonymous$$eyCheck()
     {
     //Checkif the e key is pressed 
     while (Input.Get$$anonymous$$eyDown("e"))
         {   
          //While the E key is pressed check for a key
         if (playerInventory.has$$anonymous$$ey)
         {
         Debug.Log("Door Open");
         open_door();
         yield;
          }
          else
             {
               Debug.Log("Door Denied");
               audio.clip = accessDeniedClip;
               audio.Play();
              }       
     
     }
     }
     
avatar image Tronicx · Jul 03, 2014 at 02:31 AM 0
Share

Hey man thank you very much for you help, i just need one last thing, I'm using an Inventory System form Asset Store https://www.assetstore.unity3d.com/en/#!/content/10384

I just need to add somthing to my door Script that check if i have the key in my inventory to open the door( if i have the key only) So, what should i add to check the inventory if it has like "white_key" to open the door ?

avatar image
0

Answer by Tronicx · Jul 03, 2014 at 03:51 AM

Thank you very much man you are help me alot, I just need one last thing, I'm using an Inventory System from the Asset Store https://www.assetstore.unity3d.com/en/#!/content/10384 And i don't know what to add to my Door Script that check the inventory if it has the key ( that i already looted) I just need to add something to check, if there's let say item called"white_key" then open the door if not don't I hope you could help me with that last thing :) Here's the key Script ( i don't know if that help )

 #pragma strict
 
 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;
 }
 }


And here's the Inventory Script :

 //This is the central piece of the Inventory System.
 
 var Contents : Transform[]; //The content of the Inventory
 var MaxContent : int = 12; //The maximum number of items the Player can carry.
 
 var DebugMode = false; //If this is turned on the Inventory script will output the base of what it's doing to the Console window.
 
 private var playersInvDisplay : InventoryDisplay; //Keep track of the InventoryDisplay script.
 
 static var itemHolderObject : Transform; //The object the unactive items are going to be parented to. In most cases this is going to be the Inventory object itself.
 
 @script AddComponentMenu ("Inventory/Inventory")
 
 //Handle components and assign the itemHolderObject.
 function Awake ()
 {
     itemHolderObject = gameObject.transform;
     
     playersInvDisplay = GetComponent(InventoryDisplay);
     if (playersInvDisplay == null)
     {
         Debug.LogError("No Inventory Display script was found on " + transform.name + " but an Inventory script was.");
         Debug.LogError("Unless a Inventory Display script is added the Inventory won't show. Add it to the same gameobject as the Inventory for maximum performance");
     }
 }
 
 //Add an item to the inventory.
 function AddItem(Item:Transform)
 {
     var newContents = new Array(Contents);
     newContents.Add(Item);
     Contents=newContents.ToBuiltin(Transform); //Array to unity builtin array
     
     if (DebugMode)
     {
         Debug.Log(Item.name+" has been added to inventroy");
     }
     
     //Tell the InventoryDisplay to update the list.
     if (playersInvDisplay != null)
     {
         playersInvDisplay.UpdateInventoryList();
     }
 }
 
 //Removed an item from the inventory (IT DOESN'T DROP IT).
 function RemoveItem(Item:Transform)
 {
     var newContents=new Array(Contents);
     var index=0;
     var shouldend=false;
     for(var i:Transform in newContents) //Loop through the Items in the Inventory:
     {
         if(i == Item) //When a match is found, remove the Item.
         {
             newContents.RemoveAt(index);
             shouldend=true;
             //No need to continue running through the loop since we found our item.
         }
         index++;
         
         if(shouldend) //Exit the loop
         {
             Contents=newContents.ToBuiltin(Transform);
             if (DebugMode)
             {
                 Debug.Log(Item.name+" has been removed from inventroy");
             }
             if (playersInvDisplay != null)
             {
                 playersInvDisplay.UpdateInventoryList();
             }
             return;
         }
     }
 }
 
 //Dropping an Item from the Inventory
 function DropItem(item)
 {
     gameObject.SendMessage ("PlayDropItemSound", SendMessageOptions.DontRequireReceiver); //Play sound
     
     var makeDuplicate = false;
     if (item.stack == 1) //Drop item
     {
         RemoveItem(item.transform);
     }
     else //Drop from stack
     {
         item.stack -= 1;
         makeDuplicate = true;
     }
     
     item.DropMeFromThePlayer(makeDuplicate); //Calling the drop function + telling it if the object is stacked or not.
     
     if (DebugMode)
     {
         Debug.Log(item.name + " has been dropped");
     }
 }
 
 //This will tell you everything that is in the inventory.
 function DebugInfo()
 {
         Debug.Log("Inventory Debug - Contents");
     items=0;
     for(var i:Transform in Contents){
         items++;
         Debug.Log(i.name);
     }
     Debug.Log("Inventory contains "+items+" Item(s)");
 }
 
 //Drawing an 'S' in the scene view on top of the object the Inventory is attached to stay organized.
 function OnDrawGizmos ()
 {
     Gizmos.DrawIcon (Vector3(transform.position.x, transform.position.y + 2.3, transform.position.z), "InventoryGizmo.png", true);
 }

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Check if all Materials are generated? 0 Answers

How to get a value from an array within another script. 1 Answer

How do you call from another script? 1 Answer

Attaching an RTS scrolling script to a camera (Noob Question) 3 Answers

C# Unity 3D: LookAt.cs Help please 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