Search Unity

Open and Close doors with a click

Discussion in 'Animation' started by Mery87, Sep 13, 2021.

  1. Mery87

    Mery87

    Joined:
    Oct 22, 2019
    Posts:
    9
    Good Morning, I'm Maria, and I'd like to know how to animate in 2D the doors (open and closed) whit programming.

    Thank you for the help
     

    Attached Files:

  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    I don't think you need the "Iddle" and "Open" bools in your animator. The door can either be opened or closed, so you only need the "Closed" bool. Set it to true when the door is closed, and false when the door is open.

    You can use
    OnMouseDown()
    to toggle between open and closed.

    Here is an example of what you could do:
    Code (CSharp):
    1.     Animator animator;
    2.  
    3.     void Start()
    4.     {
    5.         animator = GetComponent<Animator>();
    6.     }
    7.  
    8.     void OnMouseDown()
    9.     {
    10.         if (animator.GetBool("Closed") == true) // If the door is closed...
    11.         {
    12.             animator.SetBool("Closed", false); // Open the door.
    13.         }
    14.         else                                  // If it's not closed...
    15.         {
    16.             animator.SetBool("Closed", true); // Close the door.
    17.         }
    18.     }
    Just make sure that you have a "Box Collider 2D" component on your doors.

    Here is what your Animator Controller should look like:
    Example.JPG

    Good luck!
     
    Last edited: Sep 13, 2021
  3. Mery87

    Mery87

    Joined:
    Oct 22, 2019
    Posts:
    9
    Thank you

    And How I do to the animations only does when I click on the doors?
     
  4. Mery87

    Mery87

    Joined:
    Oct 22, 2019
    Posts:
    9
    [QUOTE = "Mery87, publicación: 7494284, miembro: 3859124"] Gracias

    I mean what do you have to do to do that the animations solo work when I do click on the doors
     
  5. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    That's my mistake, I forgot about the initial door state. You don't want the door to start animating right away, you want it to be closed at the start.

    First, make sure that "Loop Time" is unchecked for your animations; you only want them playing once.

    There are a few ways you could make the door closed at the start. One is to use an "Idle" state like you were doing before, but I think it would be much easier to add
    animator.Play("Closed", 0, 1f);
    to the Start method.

    So it would look like this:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         animator = GetComponent<Animator>();
    4.         animator.Play("Closed", 0, 1f);
    5.     }
    What this does is start the Closed animation 100% of the way through, so it starts finished. Because you don't have loop time, the door will be closed at the start, and then everything else will work normally.

    Hopefully that made sense, please let me know if you have any questions.
     
  6. Mery87

    Mery87

    Joined:
    Oct 22, 2019
    Posts:
    9
    Good Morning it give the next mistake and it happen equal
     

    Attached Files:

  7. Mery87

    Mery87

    Joined:
    Oct 22, 2019
    Posts:
    9
    i think already it works in the animator but i need to know now is how to do the animations only works when you clicks in the doors
     
  8. Mery87

    Mery87

    Joined:
    Oct 22, 2019
    Posts:
    9
    I mean only works in the animator when you do click en los bools
     
  9. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    For this line
    animator.Play("Closed", 0, 1f);
    , you need to replace "Closed" with the name of your state. In your case it would be
    animator.Play("Puerta_Closed", 0, 1f);
    .

    You need to set the bools when you click on the doors through code. I already showed the code you could use for that earlier: