CheckpointTrigger.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using TMPro;
  3. [RequireComponent(typeof(BoxCollider))]
  4. public class CheckpointTrigger : MonoBehaviour
  5. {
  6. [Header("Checkpoint Settings")]
  7. [Tooltip("Show a visual effect when checkpoint is activated")]
  8. public bool showVisualFeedback = true;
  9. [Tooltip("Only trigger once, or every time player enters")]
  10. public bool oneTimeUse = false;
  11. [Tooltip("Custom checkpoint message")]
  12. public string checkpointMessage = "Checkpoint Saved";
  13. [Header("Visual Feedback")]
  14. public GameObject checkpointVFX;
  15. public AudioClip checkpointSound;
  16. public TextMeshProUGUI checkpointText;
  17. public CanvasGroup textCanvasGroup;
  18. [Header("Fade Animation")]
  19. [Tooltip("Duration of fade in/out animation")]
  20. public float fadeDuration = 0.5f;
  21. [Tooltip("How long the message stays visible")]
  22. public float displayDuration = 2f;
  23. private bool hasBeenTriggered = false;
  24. private BoxCollider triggerCollider;
  25. private AudioSource audioSource;
  26. private void Awake()
  27. {
  28. // Ensure collider is trigger
  29. triggerCollider = GetComponent<BoxCollider>();
  30. if (triggerCollider != null)
  31. {
  32. triggerCollider.isTrigger = true;
  33. }
  34. // Setup audio source if sound is assigned
  35. if (checkpointSound != null)
  36. {
  37. audioSource = gameObject.AddComponent<AudioSource>();
  38. audioSource.clip = checkpointSound;
  39. audioSource.playOnAwake = false;
  40. }
  41. }
  42. private void OnTriggerEnter(Collider other)
  43. {
  44. if (other.CompareTag("Player"))
  45. {
  46. if (oneTimeUse && hasBeenTriggered)
  47. return;
  48. ActivateCheckpoint();
  49. }
  50. }
  51. public void ActivateCheckpoint()
  52. {
  53. CheckpointSystem checkpointSystem = CheckpointSystem.Instance;
  54. if (checkpointSystem != null)
  55. {
  56. checkpointSystem.SaveCheckpointAt(transform.position);
  57. if (showVisualFeedback)
  58. {
  59. ShowFeedback();
  60. }
  61. hasBeenTriggered = true;
  62. }
  63. }
  64. private void ShowFeedback()
  65. {
  66. if (audioSource != null && checkpointSound != null)
  67. {
  68. audioSource.Play();
  69. }
  70. if (checkpointVFX != null)
  71. {
  72. checkpointVFX.SetActive(true);
  73. Destroy(checkpointVFX, fadeDuration * 2 + displayDuration);
  74. }
  75. if (checkpointText != null && textCanvasGroup != null)
  76. {
  77. checkpointText.text = checkpointMessage;
  78. checkpointText.gameObject.SetActive(true);
  79. StartCoroutine(FadeTextInOut());
  80. }
  81. }
  82. private System.Collections.IEnumerator FadeTextInOut()
  83. {
  84. // Fade in from 0 to 1
  85. float elapsed = 0f;
  86. while (elapsed < fadeDuration)
  87. {
  88. elapsed += Time.deltaTime;
  89. textCanvasGroup.alpha = Mathf.Lerp(0f, 1f, elapsed / fadeDuration);
  90. yield return null;
  91. }
  92. textCanvasGroup.alpha = 1f;
  93. // Display at full opacity
  94. yield return new WaitForSeconds(displayDuration);
  95. // Fade out from 1 to 0
  96. elapsed = 0f;
  97. while (elapsed < fadeDuration)
  98. {
  99. elapsed += Time.deltaTime;
  100. textCanvasGroup.alpha = Mathf.Lerp(1f, 0f, elapsed / fadeDuration);
  101. yield return null;
  102. }
  103. textCanvasGroup.alpha = 0f;
  104. // Deactivate text object
  105. checkpointText.gameObject.SetActive(false);
  106. }
  107. public void ResetCheckpoint()
  108. {
  109. hasBeenTriggered = false;
  110. }
  111. private void OnDrawGizmos()
  112. {
  113. Gizmos.color = hasBeenTriggered ? Color.green : Color.yellow;
  114. Gizmos.DrawWireCube(transform.position, Vector3.one * 2f);
  115. Gizmos.color = Color.cyan;
  116. Gizmos.DrawLine(transform.position, transform.position + Vector3.up * 3f);
  117. }
  118. }