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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Door opening/closing: label seems working not right

Discussion in 'Scripting' started by pehlivan2000, Jun 21, 2015.

  1. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    Hey,

    Code (Javascript):
    1.  
    2. #pragma strict
    3.  
    4. private var guiShow : boolean = false;
    5.  
    6. var isOpen : boolean = false
    7. var door : GameObject;
    8. var rayLength = 10;
    9.  
    10. function Update() {
    11.  var hit : RaycastHit;
    12.  var fwd = transform.TransformDirection(Vector3.forward);
    13.  
    14.   if(Physics.Raycast(transform.position, fwd, hit, rayLength)) {
    15.     if(hit.collider.gameObject.tag == "Door") {
    16.       guiShow = true;
    17.       if(Input.GetKeyDown("e") && isOpen == false) {
    18.          door.GetComponent.<Animation>().Play("DoorOpen");
    19.          isOpen = true;
    20.          guiShow = false;
    21.       }
    22.       else if(Input.GetKeyDown("e") && isOpen == true) {
    23.        door.GetComponent.<Animation>().Play("DoorClose");
    24.        isOpen = false;
    25.        guiShow = false;
    26.       }
    27.     }
    28.   }
    29.   else {
    30.     guiShow = false;
    31.   }
    32. }
    33.  
    34. function OnGUI() {
    35.   if(guiShow == true && isOpen == false) {
    36.      GUI.Label(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Use Door");
    37.   }
    38. }
    39.  
    40.  
    (There shouldn't be any errors in this code! I wrote it myself, because the copy & paste from the Monoeditor didn't worked)

    The label doesn't work properly. When I look at the door, the label shows up but if I look away, the label is still there.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This took me longer than I care to admit but I finally figured it out. You're only setting guiShow to false when your physics raycast doesn't hit anything, not just when it doesn't hit a door.

    Get rid of the else statement and move it before your Raycast and you should be all set.

    Code (csharp):
    1.  
    2. guiShow = false;
    3.  
    4. if(Physics.Raycast(transform.position, fwd, hit, rayLength)) {
    5.   if(hit.collider.gameObject.tag == "Door") {
    6.     guiShow = true;
    7.  
    8.     if(Input.GetKeyDown("e") && isOpen == false) {
    9.       door.GetComponent.<Animation>().Play("DoorOpen");
    10.       isOpen = true;
    11.       guiShow = false;
    12.     }
    13.     else if(Input.GetKeyDown("e") && isOpen == true) {
    14.       door.GetComponent.<Animation>().Play("DoorClose");
    15.       isOpen = false;
    16.       guiShow = false;
    17.     }
    18.   }
    19. }
    20.  
     
  3. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    Thanks, I really appreciate your work! I'll test it right now.

    Yes, it's working. Again thanks! Just asking, if this should work if you're in a 'Trigger Zone'. Don't give me any code. I can do this myself :)
     
    Last edited: Jun 22, 2015
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You'd have to rework the logic as you shouldn't be checking for button presses inside OnTriggerEnter or OnTriggerExit, but you can definitely set up some boolean that marks if the player is "in the trigger zone" and wrap that around in your Update.

    Pseudocode:
    Code (csharp):
    1.  
    2. var triggered : bool = false;
    3.  
    4. OnTriggerEnter()
    5. {
    6.      check to make sure it's the player { triggered = true; }
    7. }
    8.  
    9. OnTriggerExit()
    10. {
    11.     check to make sure it's the player { triggered = false; }
    12. }
    13.  
    14. Update()
    15. {
    16.     guiShow = false;
    17.  
    18.     if(triggered == true)
    19.     {
    20.           all the code you have now
    21.     }
    22. }
    23.