Animation get bool doesnt work properly?

Hello everyone,
I am trying to get my door to run properly with animation, but it doesnt work out well.

I have my door animations, open and close.
So my script will check if door animation bool = false, then it sets to true, door opens. The first time works fine.But here is the problem, when I press a key again, it will go false then back to true again.

I have made sure my key is only pressed once.

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

public class DoorInteract : MonoBehaviour
{
    // Start is called before the first frame update
   

    bool Interactable;
    KeyCode inter = KeyCode.E;
    public AudioSource audio;
    [SerializeField] AudioClip door_lock;
    [SerializeField] AudioClip door_open;
    [SerializeField] AudioClip door_close;

    void Start()
    {
        Interactable = false;
        audio = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Interactable && Input.GetKeyDown(inter))
        {
                playerInteract();
        }
    }
    private void OnTriggerStay(Collider other)
    {
        if (other.tag == ("Player"))
        {
            Interactable = true;
        }
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.tag == ("Player"))
        {
            Interactable = false;
        }
    }
    void playerInteract()
    {
            if (Higashi_Door.H_Door.m_Animator.GetBool("doorOpen"))
            {
                Close();
                Debug.Log("Close");
            }
            if (!Higashi_Door.H_Door.m_Animator.GetBool("doorOpen"))
            {
                Open();
                Debug.Log("Open");
            }
       
    }

    void Open()
    {
        Higashi_Door.H_Door.m_Animator.SetBool("doorOpen", true);
        audio.PlayOneShot(door_open, 1f);
    }
    void Close()
    {
  
        Higashi_Door.H_Door.m_Animator.SetBool("doorOpen", false);
        audio.PlayOneShot(door_lock, 1f);
    }

}

I got it fixed by changing

if (!Higashi_Door.H_Door.m_Animator.GetBool("doorOpen"))

to else.

I mean they meant the same thing but why?

I don’t think they are the same thing.

I THINK that in the first code you were saying IF the door is open, then you called the function to close it. and right after that you say if its closed (or not open) (right after you just closed it) then you open it again.

the if / else combo would mean that if its open, close it, and otherwise, open it. so essentially in the first one you made two changes to the door, closing and then opening it, and in the else one where you fixed it its an either /or situation. either the door is closed (so you open it ) or open (So you closed it)

did that make sense?

1 Like

I got it now. thanks!