using UnityEngine; using System.Collections; public class Firetrap : MonoBehaviour { [SerializeField] private float damage; [Header("Firetrap Timers")] [SerializeField] private float activationDelay; [SerializeField] private float activeTime; private Animator anim; private SpriteRenderer spriteRend; private bool triggered; //when the trap gets triggered private bool active; //when the trap is active and can hurt the player private void Awake() { anim = GetComponent(); spriteRend = GetComponent(); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Player") { if (!triggered) StartCoroutine(ActivateFiretrap()); if (active) collision.GetComponent().TakeDamage(damage); } } private IEnumerator ActivateFiretrap() { //turn the sprite red to notify the player and trigger the trap triggered = true; spriteRend.color = Color.red; //Wait for delay, activate trap, turn on animation, return color back to normal yield return new WaitForSeconds(activationDelay); spriteRend.color = Color.white; //turn the sprite back to its initial color active = true; anim.SetBool("activated", true); //Wait until X seconds, deactivate trap and reset all variables and animator yield return new WaitForSeconds(activeTime); active = false; triggered = false; anim.SetBool("activated", false); } }