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);
}
}