Search Unity

Wait till the door opens and then you can close it

Discussion in 'Scripting' started by Dari, Oct 22, 2013.

  1. Dari

    Dari

    Joined:
    Mar 25, 2013
    Posts:
    130
    I want to make so I can't close the doors if they're opening, I'll have to wait till they open with full angle (90) and then I can close them, same thing with closing... If they're closing I can't open them till closing finishes.

    Current script:

    Code (csharp):
    1. var smooth = 2.0;
    2. var DoorOpenAngle = 90.0;
    3. var openSound : AudioClip;
    4. var closeSound : AudioClip;
    5. var lockedSound : AudioClip;
    6. var locked = false;
    7. var timesToOpen = 2.0;
    8.  
    9. private var open : boolean;
    10. private var enter : boolean;
    11. private var enter2 : boolean;
    12. private var gui : boolean = true;
    13. private var sound : boolean = true;
    14.  
    15. private var defaultRot : Vector3;
    16. private var openRot : Vector3;
    17.  
    18. function Start(){
    19. defaultRot = transform.eulerAngles;
    20. openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
    21. }
    22.  
    23. //Main function
    24. function Update (){
    25.  
    26. TimesToOpen();
    27.  
    28. if(open){
    29. //Open door
    30. transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
    31. }else{
    32. //Close door
    33. transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
    34. }
    35.  
    36. OpenDoors();
    37.  
    38. if(sound){
    39. if(Input.GetMouseButtonDown(0)  enter  open){
    40. AudioSource.PlayClipAtPoint(openSound, transform.position);
    41. }
    42.  
    43. if(Input.GetMouseButtonDown(0)  enter  !open){
    44. AudioSource.PlayClipAtPoint(closeSound, transform.position);
    45. }
    46.  
    47. if(Input.GetMouseButtonDown(0)  locked  enter2){
    48. AudioSource.PlayClipAtPoint(lockedSound, transform.position);
    49. }
    50. }
    51. }
    52.  
    53. function OnGUI(){
    54. if(locked  enter2){
    55. open = false;
    56. enter = false;
    57. GUI.Box(new Rect(Screen.width/2 - 65, Screen.height - 100, 135, 25), "The door is locked");
    58. }
    59. if(gui){
    60. if(enter  !open){
    61. GUI.Box(new Rect(Screen.width/2 - 165, Screen.height - 100, 350, 25), "Press (Left Mouse Button) to open the door");
    62. }
    63. if(enter  open){
    64. GUI.Box(new Rect(Screen.width/2 - 165, Screen.height - 100, 350, 25), "Press (Left Mouse Button) to close the door");
    65. }
    66. }
    67. }
    68.  
    69. //Activate the Main function when player is near the door
    70. function OnTriggerEnter (other : Collider){
    71. if (other.gameObject.tag == "Player") {
    72. enter = true;
    73. enter2 = true;
    74. showFinish = true;
    75. }
    76. }
    77.  
    78. //Deactivate the Main function when player is go away from door
    79. function OnTriggerExit (other : Collider){
    80. if (other.gameObject.tag == "Player") {
    81. enter = false;
    82. enter2 = false;
    83. showFinish = false;
    84. }
    85. }
    86.  
    87. function TimesToOpen(){
    88.  
    89. if(timesToOpen <= 0){
    90. open = false;
    91. gui = false;
    92. sound = false;
    93. }
    94. }
    95.  
    96. function OpenDoors(){
    97. if(Input.GetMouseButtonDown(0)  enter){
    98. open = !open;
    99. timesToOpen -= 1.0;
    100. }
    101. }
    Please help me, thanks in advance! :D
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You'll want to look into Coroutines, then make two of them.

    One to open the door, one to close the door.

    Create a boolean that controls whether or not the door is interactive (I'll call it isInteractive)

    Then make another function (also a coroutine); I'll call it Interact

    When the mouse is clicked, use StartCoroutine (Interact ()) only if isInteractive is true

    Inside Interact, set isInteractive to false then yield for either the open coroutine or close coroutine (depending on door state). When the door coroutine is finished (meaning logic is passed back to Interact after the yield statement) set isInteractive back to true.
     
  3. Dari

    Dari

    Joined:
    Mar 25, 2013
    Posts:
    130
    Is it supposed to look like this:

    Code (csharp):
    1. function Interact(){
    2. if(Input.GetMouseButtonDown(0)  enter){
    3. StartCoroutine (Interact (isInteractive == true));
    4. yield WaitForSeconds(1);
    5. isInteractive = false;
    6. yield WaitForSeconds(1);
    7. isInteractive = true;
    8. }
    9. }
    Probably not, because it won't work. Help :(
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    No, it would be something along the lines of this (not full code!):
    Code (csharp):
    1.  
    2. function Update () {
    3.     if (Input.GetMouseButtonDown (0)) {
    4.         if (isInteractive) {
    5.             StartCoroutine (Interact ());
    6.         }
    7.     }
    8. }
    9.  
    10. function Interact () {
    11.     isInteractive = false;
    12.    
    13.     if (doorOpen) {
    14.         yield StartCoroutine (CloseDoor ());
    15.     } else {
    16.         yield StartCoroutine (OpenDoor ());
    17.     }
    18.  
    19.     doorOpen = !doorOpen;
    20.  
    21.     isInteractive = true;
    22. }
    23.