Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Door opens only if i have key in inventory

Discussion in 'Editor & General Support' started by Tronicx, Jul 4, 2014.

  1. Tronicx

    Tronicx

    Joined:
    Mar 30, 2014
    Posts:
    4
    Greetings... I'm using the Inventory in the Asset Store https://www.assetstore.unity3d.com/en/#!/content/10384

    And i made an open-able door and a loot-able key, I just need to add somthing to my door Script to make it opens only i have a key in my inventory with a name like "white_key" If anyone can help me please ?

    Here's my Door JavaScript :


    Code (JavaScript):
    1. #pragma strict
    2. var smooth = 2.0;
    3. var DoorOpenAngle = 90.0;
    4. private var open : boolean;
    5. private var enter : boolean;
    6. private var defaultRot : Vector3;
    7. private var openRot : Vector3;
    8. function Start(){
    9. defaultRot = transform.eulerAngles;
    10. openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
    11. }
    12. //Main function
    13. function Update (){
    14. if(open){
    15. //Open door
    16. transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
    17. }else{
    18. //Close door
    19. transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
    20. }
    21. if(Input.GetKeyDown("f") && enter){
    22. open = !open;
    23. }
    24. }
    25. function OnGUI(){
    26. if(enter){
    27. GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
    28. }
    29. }
    30. //Activate the Main function when player is near the door
    31. function OnTriggerEnter (other : Collider){
    32. if (other.gameObject.tag == "Player") {
    33. enter = true;
    34. }
    35. }
    36. //Deactivate the Main function when player is go away from door
    37. function OnTriggerExit (other : Collider){
    38. if (other.gameObject.tag == "Player") {
    39. enter = false;
    40. }
    41. }
     
  2. iamsteele

    iamsteele

    Joined:
    Mar 25, 2014
    Posts:
    29
    at first glance it looks as though you are keeping the door held open and held shut depending on what the boolean open is set to.... logically this doesn't make sense.. technically in this tiny example it's OK code but in reality opening the door should be event driven... only executing while its state it set to "opening" or "closing" and only started when a "open" or "close" methods are called from update when input and canOpen are both set to true. I could have put that into code, but if you read through the paragraph it'll help you understand how to look at your code and figure things out for yourself in a more logical manner.

    you could also rename "enter" to closeEnoughToOpen to be more descriptive... or canOpen or withinRange or something.
    you could also rename "open" to doorState for the same reason..
     
  3. Tronicx

    Tronicx

    Joined:
    Mar 30, 2014
    Posts:
    4
    Thank you for replaying, I know I'm in a mess and code is mess :D but as you see I'm new to all this ( Unity and coding)

    My only problem now is : How to make the Door check the Inventory if there's the key So the door opens and if there's no key the door don't open.

    Can you help me with that please ?
     
  4. iamsteele

    iamsteele

    Joined:
    Mar 25, 2014
    Posts:
    29
    all you need to do is reference the inventory component on your player and loop through the Inventory Contents transforms and pull the information name off each one. Set a bool before the loop to false, and on a name match in the loop, set the bool to true. after the loop do an if statement to check if the bool was set to true, and if so, open the door.

    To make it easy on yourself, take a look at the code in InventoryDisplay.cs for inspiration on how to reference the inventory component and how to do the loop and how to get the name.
     
  5. iamsteele

    iamsteele

    Joined:
    Mar 25, 2014
    Posts:
    29
    everything you will ever do in code, until you get up to optimizations, is reference things, and get information out of them. Sometimes you will have to write the getters and setters (methods that return what you want out of one script back to whatever script calls that method) yourself and sometimes you'll have to make the arrays containing references yourself.. practice thinking of the code in that way, and it all becomes the same thing, with different names. Makes object oriented coding a lot easier.
     
  6. Tronicx

    Tronicx

    Joined:
    Mar 30, 2014
    Posts:
    4
    I opened all codes in Notpad++ and red it more than one time, I know i need like one line something like " If (PlayerInventory.gameobject.white_key = true) , then the door opening code, but still can't know how to refer to the Inventory :(
     
  7. iamsteele

    iamsteele

    Joined:
    Mar 25, 2014
    Posts:
    29
    The contents of the inventory are stored in the script Inventory under the variable Contents. It lists them as Transforms. You reference them by

    var Contents[] = GetComponent<Inventory>().Contents

    you step through that array in a for loop like this...

    for(var i = 0; i < Contents.Length; i++) {
    //do if statement here
    }

    and compare the names of the transforms like this:

    if(Contents.name == "white_key") {
    //then do this
    }

    I only code in C#, not JS, so I can't post the exact syntax here, but you get the idea.