CheckpointTrigger.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using UnityEngine;
  2. /// <summary>
  3. /// Place this component on a GameObject to create a checkpoint zone.
  4. /// When the player enters, it will save the current game state.
  5. /// </summary>
  6. [RequireComponent(typeof(BoxCollider))]
  7. public class CheckpointTrigger : MonoBehaviour
  8. {
  9. [Header("Checkpoint Settings")]
  10. [Tooltip("Show a visual effect when checkpoint is activated")]
  11. public bool showVisualFeedback = true;
  12. [Tooltip("Only trigger once, or every time player enters")]
  13. public bool oneTimeUse = false;
  14. [Tooltip("Custom checkpoint message")]
  15. public string checkpointMessage = "Checkpoint Saved";
  16. [Header("Visual Feedback")]
  17. public GameObject checkpointVFX;
  18. public AudioClip checkpointSound;
  19. private bool hasBeenTriggered = false;
  20. private BoxCollider triggerCollider;
  21. private AudioSource audioSource;
  22. private void Awake()
  23. {
  24. // Ensure collider is trigger
  25. triggerCollider = GetComponent<BoxCollider>();
  26. if (triggerCollider != null)
  27. {
  28. triggerCollider.isTrigger = true;
  29. }
  30. // Setup audio source if sound is assigned
  31. if (checkpointSound != null)
  32. {
  33. audioSource = gameObject.AddComponent<AudioSource>();
  34. audioSource.clip = checkpointSound;
  35. audioSource.playOnAwake = false;
  36. }
  37. }
  38. private void OnTriggerEnter(Collider other)
  39. {
  40. // Check if player entered
  41. if (other.CompareTag("Player"))
  42. {
  43. // If one-time use and already triggered, ignore
  44. if (oneTimeUse && hasBeenTriggered)
  45. return;
  46. ActivateCheckpoint();
  47. }
  48. }
  49. /// <summary>
  50. /// Activate this checkpoint and save game state
  51. /// </summary>
  52. public void ActivateCheckpoint()
  53. {
  54. CheckpointSystem checkpointSystem = CheckpointSystem.Instance;
  55. if (checkpointSystem != null)
  56. {
  57. // Save checkpoint at this position
  58. checkpointSystem.SaveCheckpointAt(transform.position, transform.rotation);
  59. Debug.Log($"[Checkpoint] {checkpointMessage} at {transform.position}");
  60. // Show feedback
  61. if (showVisualFeedback)
  62. {
  63. ShowFeedback();
  64. }
  65. hasBeenTriggered = true;
  66. }
  67. else
  68. {
  69. Debug.LogWarning("[Checkpoint] CheckpointSystem not found!");
  70. }
  71. }
  72. private void ShowFeedback()
  73. {
  74. // Play sound
  75. if (audioSource != null && checkpointSound != null)
  76. {
  77. audioSource.Play();
  78. }
  79. // Spawn VFX
  80. if (checkpointVFX != null)
  81. {
  82. GameObject vfx = Instantiate(checkpointVFX, transform.position, Quaternion.identity);
  83. Destroy(vfx, 3f); // Auto-cleanup after 3 seconds
  84. }
  85. // Optional: Show UI message (you can implement this with your UI system)
  86. Debug.Log($"[Checkpoint] ✓ {checkpointMessage}");
  87. }
  88. /// <summary>
  89. /// Reset this checkpoint so it can be triggered again
  90. /// </summary>
  91. public void ResetCheckpoint()
  92. {
  93. hasBeenTriggered = false;
  94. }
  95. // Visualize checkpoint in editor
  96. private void OnDrawGizmos()
  97. {
  98. Gizmos.color = hasBeenTriggered ? Color.green : Color.yellow;
  99. Gizmos.DrawWireCube(transform.position, Vector3.one * 2f);
  100. Gizmos.color = Color.cyan;
  101. Gizmos.DrawLine(transform.position, transform.position + Vector3.up * 3f);
  102. }
  103. }