top of page
Search

Drawer Clip

  • Writer: Keshav Batra
    Keshav Batra
  • Dec 3, 2021
  • 1 min read

So after weeks of trying to figure it out, I finally got my drawer to open and close on command. What I had to do was make sure that the animations were named correctly and that the functions were true!




{
    [SerializeField] private Animator myDrawer = null;
    [SerializeField] private bool openTrigger = false;
    [SerializeField] private bool closeTrigger = false;
    private bool DrawerOpen = false;
    private bool DrawerClose = true;
    private bool IsPlayerInTrigger;


    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.I) && IsPlayerInTrigger) //Checking to see if the player is in the trigger and pressing I
        {
            if (DrawerClose) //Checking to see if the drawer is closed
            {
                DrawerClose = false; //Drawer is open
                myDrawer.SetBool("IsOpen",true); //Will play open animation
            }

            else
            {
                DrawerClose = true;// Drawer is closed
                myDrawer.SetBool("IsOpen",false); //Will play the close animation
            }
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            IsPlayerInTrigger = true;
            Debug.Log("InTheTrigger");
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            myDrawer.SetBool("IsClosed", true);
            IsPlayerInTrigger = false;
            Debug.Log("OutOfTheTrigger");
        }
    }
}


 
 
 

Comments


bottom of page