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 opener script issues with closing on 2nd click

Discussion in 'Scripting' started by GFFG, May 10, 2016.

  1. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Hi,

    I've got a door with open and closing animation and a button to activate it. I click the button once ande the door opens, but how do I make it so that the door closes again, once it's been opened, when I click the same button again?

    This is my script on the button that should open and close the door:

    Code (CSharp):
    1. void OnMouseOver ()
    2.     {
    3.         if (Input.GetMouseButton (0)) {
    4.             anim.SetBool ("ldbuttonpress", true);
    5.             anim.SetBool ("ldidle", false);
    6.             DoorRight.GetComponent<Animator> ().Play ("RDopen");
    7.  
    8.  
    9.         }
    10.    
    11.  
    12.  
    13.          else {
    14.             anim.SetBool ("ldbuttonpress", false);
    15.             anim.SetBool ("ldidle", true);
    16.  
    17.  
    18.  
    19.  
    20.         }
    21.     }
    the bools are just the buttons animation :) The 'else' works on the button script but it wouldn't work on the door. All help is much appreciated
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    personally I'd separate out the animation handling and the doorstate handling
    Code (csharp):
    1.  
    2. // pseudo
    3. bool doorState;
    4.  
    5. void ButtonClickedCalledFunction() // or whatever it's called :D
    6. {
    7.     doorState = !doorState;
    8.     UpdateAnimationStates(doorState);
    9. }
    10.  
    11. void UpdateAnimationStates(bool s)
    12. {
    13.     if(s)
    14.     {
    15.         // open stuff
    16.     }
    17.     else
    18.     {
    19.         //closed stuff
    20.     }
    21. }
    22.  
     
  3. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    H
    Hey LeftyRighty,

    Thanks for your help, I can't really implement it though, but I'll play around with it and hopefully understand how it's supposed to be implemented with my initial code.