Hi everyone, I have a problem with the code I wrote. According to the logic of the code, the player should open the door when he presses the right key, however when the player presses the key to open the door, the other doors open also. How can I make sure that only the door concerned is opened?
public class Door : MonoBehaviour
{
private bool doorOpen = false;
private Animator animator;
public Canvas canvas;
// Start is called before the first frame update
void Awake()
{
animator = GetComponent<Animator>();
canvas.gameObject.GetComponent<Canvas>();
if (canvas.gameObject.activeSelf == true)
{
canvas.gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
if(canvas.gameObject.activeSelf == true && Input.GetAxis("PickUp") != 0)
{
StartCoroutine("ChangeDoorState");
}
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
canvas.gameObject.SetActive(true);
Debug.Log("Sta toccando");
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
canvas.gameObject.SetActive(false);
Debug.Log("Non Sta toccando");
}
}
public void OnTriggerStay(Collider other)
{
if(other.gameObject.tag == "Player" && doorOpen == true)
{
canvas.gameObject.SetActive(false);
}
if (other.gameObject.tag == "Player" && doorOpen == false)
{
canvas.gameObject.SetActive(true);
}
}
public IEnumerator ChangeDoorState()
{
if(doorOpen == false)
{
animator.SetBool("open", true);
doorOpen = true;
yield return new WaitForSeconds(5);
animator.SetBool("open", false);
}
}
}