revival/game/Assets/Scripts/PressurePlate/PressurePlate.cs

58 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class PressurePlate : MonoBehaviour
{
[SerializeField] private float _triggerDelay = 2.0f;
[SerializeField] private Animator pPlateAnim = null;
[SerializeField] private UnityEvent _onPress;
private bool opened;
private float? triggered = null;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player") && !opened)
{
//pressure plate down and start trigger time
pPlateAnim.Play("PPdown", 0, 0f);
triggered = Time.time;
}
}
private void OnTriggerExit(Collider other)
{
if(other.CompareTag("Player"))
{
if(triggered!=null && !opened)
{
triggered = null;
pPlateAnim.Play("PPup", 0, 0f);
//reset platform position
// _onPress.Invoke();
}
}
}
private void Update()
{
var timepassed = Time.time - triggered;
if(timepassed > _triggerDelay && !opened)
{
_onPress.Invoke();
opened = true;
}
}
public void plateUp()
{
pPlateAnim.Play("PPup", 0, 0);
triggered = null;
opened = false;
}
}