Scriptum/Assets/OpenPanelOnCollision.cs
2022-06-12 17:18:43 +02:00

123 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpenPanelOnCollision : MonoBehaviour
{
public GameObject Panel;
public GameObject OtherPanel;
public GameObject SecondPanel;
public GameObject PanelAbove;
public GameObject obj;
private bool inRange = false;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Player")
{
inRange = true;
if(SecondPanel.active == false)
{
Panel.SetActive(true);
}
else
{
PanelAbove.SetActive(true);
}
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.tag == "Player")
{
inRange = true;
if(OtherPanel.active == true)
{
if (SecondPanel.active == false)
{
Panel.SetActive(false);
}
else
{
SecondPanel.SetActive(false);
PanelAbove.SetActive(false);
}
}
else if (SecondPanel.active == false)
{
Panel.SetActive(true);
}
else
{
PanelAbove.SetActive(true);
}
}
}
IEnumerator Timer1()
{
yield return new WaitForSeconds(0.3f);
if(inRange == false)
{
Panel.SetActive(false);
}
}
IEnumerator Timer2()
{
yield return new WaitForSeconds(0.3f);
if (inRange == false)
{
PanelAbove.SetActive(false);
}
}
void Update()
{
if(obj.active == false)
{
if (Panel.active == true)
{
Panel.SetActive(false);
}
else if (PanelAbove.active == true)
{
PanelAbove.SetActive(false);
}
}
if(OtherPanel.active == true)
{
StartCoroutine(Timer1());
}
if(SecondPanel.active == false)
{
if(PanelAbove.active == true)
{
StartCoroutine(Timer2());
PanelAbove.SetActive(false);
Panel.SetActive(true);
}
}
}
private void OnTriggerExit2D(Collider2D collision)
{
inRange = false;
if(Panel.active == true)
{
StartCoroutine(Timer1());
}
else if(PanelAbove.active == true)
{
StartCoroutine(Timer2());
}
}
}