Search Unity

Unity2D How to Animate a Door Opening via Press of a Button

Discussion in 'Scripting' started by cadekatharina, Jul 7, 2019.

  1. cadekatharina

    cadekatharina

    Joined:
    Jun 24, 2019
    Posts:
    2
    Alright, this is a question that has been asked so many times it's gotten old, but I'm stuck and haven't been able to find any solution after searching for hours. I just started working with Unity in hopes of making a 2D game, and so far have been able to get the character controller to move at the press of a button as well as assigning a walking animation. But I am unable to script a door to open.

    I've searched the Unity forum as well as YouTube, but so far, none of the scripts I tried to implement off tutorials, etc. have worked for me. I'm about ready to tear my hair out. I don't know if I'm missing something or just doing something wrong.

    The run-down is this: I've got a set of animations for the door. One is a set idle state (to fall back to just in case), the other an opening animation, and the other a closing one. The player character has been given a box collider and the default sprite of the door has been given one as well. I've set up, in the animator for the door, that the idle animation will change to open via the condition/parameter of pressing the E button. Now, I know you have to translate that into a script function, but I've NO IDEA where to start.

    As I've just started scripting (I'm literally two days into learning Unity, and maybe I'm just not cut out for this scripting stuff), I am super lost on how to get the door to animate when the player presses e when in range of the door's collider. I'm at my wits end but don't want to give up now just because I've hit a snag I don't understand.

    In any case, I hope that makes sense, and I'd really appreciate the help. This is a huge hurdle for me, but something I really want to accomplish.
     
  2. Okay, since you want to learn, I won't give you any exact solution, but I will describe what you need to do, so you can build for yourself and when you do and you have more questions you can come back with them and you can ask concrete ones.

    So, think about the things separately first.
    - you have your player. what it can do? it can move and it can collide (among other things, but these are the only ones which is important for the door mechanic)
    - you have a door
    it has the following states:
    - closed (default)
    - opening (during the opening animation)
    - opened
    - closing (if you want)

    So, next, when and what state changes you want?
    - closed state: player bumps into the door and press the 'E' start playing the door opening animation and set the door's state to opening
    - opening: when the door opening animation ends the state should switch to opened without any other intervention
    - opened: (player can walk through without stopping in this state) after a timer expires start the closing animation and set the state to closing
    - closing: when the door closing animation ends set the state automatically to closed without interaction

    Of course if you only want to open the door and never close, you can cut out the last two points entirely.

    Simple solution:
    - you put trigger on the door and when the player OnTriggerEnter you set a boolean to true (playerInDoorOpenerArea or something), when the player leaves, you set it false
    - when the player hit 'E' AND the playerInDoorOpenerArea is true, start the door's animation and set the state to opening
    - the opening animation needs to have an event at the end (or you can invoke a function roughly the same time as the animation plays) to set the opening state to opened
    - if the player bumps in the door and the state is opened, you allow the player to go through

    So basically that is it. You can build it more sophisticated but you will learn that over time. For starting it is good enough.
    Let us know how it went, and if you stuck somewhere, feel free to post more questions, you can post your scripts too and we can help to fix them or guide you how to fix it.

    Good luck with your learning and keep it up!
     
    Last edited by a moderator: Jul 7, 2019
  3. cadekatharina

    cadekatharina

    Joined:
    Jun 24, 2019
    Posts:
    2
    Thank you so much for the assistance! Following your advice, I wanted to break it down into pieces, and decided to start working on just getting the door to open first. Though I'm new to this script writing thing, I constructed a very small script in an attempt to get the door to play its opening animation when the player is near the door, and the E button is pressed.

    I walk the character up to the door, but when I press the E button, the animation doesn't play. I'm unsure if it even recognizes the colliders...

    Needless to say, I'm not very good at it, and nothing worked. But I'd like to know where I went wrong.

    I've attached my very simple script down below, and am curious as to whether the syntax, the command choice, or wording is just wrong. I'm just trying to start small and get the door to animate open on command, and plan to set up the closing animation later.

    This is the first script I've written by hand, so... there's probably quite a few things wrong. I feel like the collider isn't set right as well as the input key/bool and was hoping to get some advice on how to fix it!

    Thank you very much!



    Code (CSharp):
    1. public class doorScript : MonoBehaviour
    2. {
    3.     bool PlayerInDoorArea;
    4.    
    5.     public Animator anim;
    6.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         anim = GetComponent<Animator>();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         // this is where I'm having problems
    17.         void OnTriggerEnter(Collider other)
    18.         {
    19.             if (other.gameObject.name == "Player")
    20.                 if (Input.GetKey("E"))
    21.                 {
    22.                     bool PlayerInDoorArea = true;
    23.                     anim.Play("doorOpen");
    24.                 }
    25.            
    26.         }
    27.     }  
    28. }