using System.Collections; using System.Collections.Generic; using UnityEngine; public class Teleporter : MonoBehaviour { private GameObject currentTeleporter; //teleporter in use private bool isCooldown = false; //is the cooldown active? public float cooldownDuration = 1f; // Duration of the cooldown in seconds private float cooldownTimer = 0f; //timer to keep track of cooldown time void Update() { // Update cooldown timer if (isCooldown) { cooldownTimer -= Time.deltaTime; //decreasing the timer by the time passed since last frame if (cooldownTimer <= 0f) { isCooldown = false; //end the cooldown } } // Teleport if currentTeleporter is set and cooldown is not active if (currentTeleporter != null && !isCooldown) { transform.position = currentTeleporter.GetComponent().getDestination().position; StartCooldown(); //start cooldown after teleporting } } void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Teleporter")) //check if the player enters a teleporter trigger { currentTeleporter = collision.gameObject; //set the current teleporter AudioManager.Instance.PlaySFX("Teleporter"); //play sound effect } } void OnTriggerExit2D(Collider2D collision) { if (collision.CompareTag("Teleporter")) //check if the player exits a teleporter { if (collision.gameObject == currentTeleporter) //ensure the teleporters match up { currentTeleporter = null; //Reset the current teleporter } } } void StartCooldown() { isCooldown = true; //activate the cooldown cooldownTimer = cooldownDuration; //set the timer to the cooldown duration } }