| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using UnityEngine;
- /// <summary>
- /// Place this component on a GameObject to create a checkpoint zone.
- /// When the player enters, it will save the current game state.
- /// </summary>
- [RequireComponent(typeof(BoxCollider))]
- public class CheckpointTrigger : MonoBehaviour
- {
- [Header("Checkpoint Settings")]
- [Tooltip("Show a visual effect when checkpoint is activated")]
- public bool showVisualFeedback = true;
-
- [Tooltip("Only trigger once, or every time player enters")]
- public bool oneTimeUse = false;
-
- [Tooltip("Custom checkpoint message")]
- public string checkpointMessage = "Checkpoint Saved";
-
- [Header("Visual Feedback")]
- public GameObject checkpointVFX;
- public AudioClip checkpointSound;
-
- private bool hasBeenTriggered = false;
- private BoxCollider triggerCollider;
- private AudioSource audioSource;
- private void Awake()
- {
- // Ensure collider is trigger
- triggerCollider = GetComponent<BoxCollider>();
- if (triggerCollider != null)
- {
- triggerCollider.isTrigger = true;
- }
-
- // Setup audio source if sound is assigned
- if (checkpointSound != null)
- {
- audioSource = gameObject.AddComponent<AudioSource>();
- audioSource.clip = checkpointSound;
- audioSource.playOnAwake = false;
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- // Check if player entered
- if (other.CompareTag("Player"))
- {
- // If one-time use and already triggered, ignore
- if (oneTimeUse && hasBeenTriggered)
- return;
-
- ActivateCheckpoint();
- }
- }
- /// <summary>
- /// Activate this checkpoint and save game state
- /// </summary>
- public void ActivateCheckpoint()
- {
- CheckpointSystem checkpointSystem = CheckpointSystem.Instance;
-
- if (checkpointSystem != null)
- {
- // Save checkpoint at this position
- checkpointSystem.SaveCheckpointAt(transform.position, transform.rotation);
-
- Debug.Log($"[Checkpoint] {checkpointMessage} at {transform.position}");
-
- // Show feedback
- if (showVisualFeedback)
- {
- ShowFeedback();
- }
-
- hasBeenTriggered = true;
- }
- else
- {
- Debug.LogWarning("[Checkpoint] CheckpointSystem not found!");
- }
- }
- private void ShowFeedback()
- {
- // Play sound
- if (audioSource != null && checkpointSound != null)
- {
- audioSource.Play();
- }
-
- // Spawn VFX
- if (checkpointVFX != null)
- {
- GameObject vfx = Instantiate(checkpointVFX, transform.position, Quaternion.identity);
- Destroy(vfx, 3f); // Auto-cleanup after 3 seconds
- }
-
- // Optional: Show UI message (you can implement this with your UI system)
- Debug.Log($"[Checkpoint] ✓ {checkpointMessage}");
- }
- /// <summary>
- /// Reset this checkpoint so it can be triggered again
- /// </summary>
- public void ResetCheckpoint()
- {
- hasBeenTriggered = false;
- }
- // Visualize checkpoint in editor
- private void OnDrawGizmos()
- {
- Gizmos.color = hasBeenTriggered ? Color.green : Color.yellow;
- Gizmos.DrawWireCube(transform.position, Vector3.one * 2f);
-
- Gizmos.color = Color.cyan;
- Gizmos.DrawLine(transform.position, transform.position + Vector3.up * 3f);
- }
- }
|