Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Trying to combine this script for a Security Door Code

Discussion in 'Scripting' started by gothicplagueltd, Jun 6, 2013.

  1. gothicplagueltd

    gothicplagueltd

    Joined:
    Apr 29, 2013
    Posts:
    7
    I am trying to interact these codes I found to make a door where you have to enter a code before being able to open it. The GUI only pops up when you bump into a collider. I am trying to revamp these codes into one, I am prolly making it a lot harder then need be. Only useful help please, dont be condescending.

    Door Script

    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;
    }
    }

    Collide Script (Figure I Can Alter This To Let Them Know To Enter The Code)

    var myGUIText : GUIText;


    function Start ()
    {
    myGUIText.gameObject.active = false;
    }

    function OnTriggerEnter()
    {
    myGUIText.text = "Oops! Dead end. Keep trying!";
    myGUIText.gameObject.active = true;
    }

    function OnTriggerExit()
    {
    myGUIText.gameObject.active = false;
    }

    Door Code

    var doorCode : String = "1234";
    var stringToEdit : String;
    var buttonMessage : String = "Access Denied";

    function OnGUI(){
    stringToEdit = GUI.TextField (Rect (20, 100, 70, 25), stringToEdit, 4);
    if(stringToEdit == doorCode){
    buttonMessage = "OpenDoor";
    }else{
    buttonMessage = "Access Denied";
    }
    if(GUI.Button(Rect(95, 100, 95, 35), buttonMessage) stringToEdit == doorCode){
    //openDoor
    }
    }



    Thank you in advance, I dont want people to write it for me, if you can help me a little please. I am stuck on this part and can not move on with my Horror FPS without this code.
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    To use code tags just hit Go Advanced at the bottom right. Makes your code easier to read and find errors. Speaking of errors, what kind are u getting? Using print or Debug will help u troubleshoot to see what parts of your script may not be working, then you only need to post the broken / error filled part of the script.

    If someone doesn't beat me to it ill take a look at your script in a min.

    Here you go. :D

    // May not be exactly what you were looking for but has all the features from the examples.

    Code (csharp):
    1.  
    2. var pWord : String = "1234";
    3. var usePassWord : boolean;
    4. var myGUIText : GUIText;
    5.  
    6. private var openDoor : boolean;
    7. private var enterZone : boolean;
    8. private var openRot = Vector3(0,120,0);
    9. private var defaultRot = Vector3(0,0,0);
    10. private var doorCode : String = "1234";
    11. private var buttonMessage : String = "Access Denied";
    12. ///=========================================================================///
    13. function Start(){
    14.     //------------//
    15.     defaultRot = transform.eulerAngles;
    16.     //------------//
    17.     if(myGUIText) { myGUIText.gameObject.active = false; }
    18.     //------------//
    19.     openRot = new Vector3(defaultRot.x, defaultRot.y + openRot.y, defaultRot.z);
    20.     //------------//
    21. }
    22. ///========================================================================================///
    23. function Update (){
    24.     //------------//
    25.     if(openDoor){  
    26.       transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * 2); // Open door
    27.     }
    28.     else{
    29.       transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * 2); // Close door
    30.     }
    31.     //------------// Not being used on a key code door
    32.     if(!usePassWord) {
    33.         if(Input.GetKeyDown("f")  enterZone){
    34.           openDoor = !openDoor;
    35.         }
    36.     }
    37.     //------------//
    38. }
    39. ///========================================================================================///
    40. function OnTriggerEnter (other : Collider){
    41.     //------------//
    42.     if(myGUIText) { myGUIText.text = "Oops! Dead end. Keep trying!"; }
    43.     if(myGUIText) { myGUIText.gameObject.active = true; }
    44.     //------------//
    45.     if (other.gameObject.tag == "Player") {
    46.       enterZone = true;
    47.     }
    48.     //------------//   
    49. }
    50. ///========================================================================================///
    51. function OnTriggerExit (other : Collider){
    52.     //------------//
    53.     if(myGUIText) { myGUIText.text = ""; }
    54.     if(myGUIText) { myGUIText.gameObject.active = false; }
    55.     //------------//
    56.     if (other.gameObject.tag == "Player") {
    57.       enterZone = false;
    58.     }
    59.     //------------//
    60. }
    61. ///========================================================================================///
    62. function OnGUI(){
    63.     //------------//
    64.     pWord = GUI.TextField (Rect (20, 100, 70, 25), pWord, 4);
    65.     //------------//
    66.     if(pWord == doorCode){
    67.       buttonMessage = "OpenDoor";
    68.     }
    69.     else{
    70.       buttonMessage = "Access Denied";
    71.     }
    72.     if(GUI.Button(Rect(95, 100, 95, 35), buttonMessage)  pWord == doorCode){
    73.       openDoor = true;
    74.     }
    75.     //------------//
    76.     if(usePassWord) {
    77.         if(enterZone){
    78.           GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 250, 30), "Enter Key Code Then Press 'F' to open");
    79.         }
    80.     }
    81.     else {
    82.         if(enterZone){
    83.           GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 250, 30), "Press 'F' to open the door");
    84.         }
    85.     }
    86.     //------------//
    87. }
    88. ///========================================================================================///
    89.  
     
    Last edited: Jun 6, 2013
  3. gothicplagueltd

    gothicplagueltd

    Joined:
    Apr 29, 2013
    Posts:
    7
    Thank you, I am trying to get it to work now. It functions properly I am just trying to adjust it so the GuiText isnt up all the time and the door can close. I do appreciate the help though