'Animation on a door

I've been studying c sharp as part of my games course and my teacher has left and they cant find a replacement so I'm really struggling with coding my demo game. I’ve got a problem with my doors in my game and I was wondering if anyone knew how u can get it to work because I haven’t really got a clue when it comes to coding. Basically what I’ve got is my doors are shut then when I start my game they open and then shut but I need a code that will make sure it says shut until I’m in the trigger point but all the YouTube clips haven't been working and it says it’s got problems with it. I know what I want it to do I just don’t know how to put that into action.

This is my code but I don't think it does anything:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class  DoorAnimation: MonoBehaviour
{
    Animator animator;
    int isOpeningHash;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        animator = GetComponent<Animator>();
        isOpeningHash = Animator.StringToHash("isOpening");

        bool isOpening = animator.GetBool(isOpeningHash);
        bool OpenPressed = Input.GetKey("e");
        bool CloseDoor = Input.GetKey("left shift");

        if (!isOpening && OpenPressed)
        {
            animator.SetBool(isOpeningHash, true);
        }

        if (isOpening && OpenPressed)
        {
            animator.SetBool(isOpeningHash, false);
        }
    }
}


Solution 1:[1]

First of all, make 2 animations(and turn off the loop or it will loop for eternity) for the opening and closing of door. Don't make Bools for the animator. It's basically useless. At the top of a script declare a bool called as open and set it equal to false(if the door is closed from start). Check if E is pressed and open is set to false(the door is closed), play the opening animation, set open to true and if left shift is pressed and open is set to true(the door is open) play the closing animation, set open to false. You can play animations if a certain key is pressed like this https://answers.unity.com/questions/1362883/how-to-make-an-animation-play-on-keypress-unity-ga.html This should do your job :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1