using System; using UnityEngine; namespace RR.Game2D.Entities { [RequireComponent(typeof(Collider2D))] public class Checkpoint : MonoBehaviour { [Tooltip("Уникальное имя в пределах сцены (например CP_01). Если пусто — возьмём имя объекта.")] public string checkpointId; [Tooltip("Опционально: куда и как смотреть после респавна.")] public Vector2 spawnForward = Vector2.right; [SerializeField, Tooltip("Тег объекта игрока, который активирует чекпоинт")] private string playerTag = "Player"; [Header("FX (необязательно)")] public GameObject activateVfx; public AudioClip activateSfx; bool _activated; void Reset() { var col = GetComponent(); col.isTrigger = true; } void Awake() { if (String.IsNullOrWhiteSpace(checkpointId)) checkpointId = gameObject.name; } void OnTriggerEnter2D(Collider2D other) { if (_activated) return; // Определяем объект-источник (учтём Rigidbody, если он есть) var source = other.attachedRigidbody ? other.attachedRigidbody.gameObject : other.gameObject; if (!source.CompareTag(playerTag)) return; _activated = true; if (CheckpointSystem.Instance) CheckpointSystem.Instance.SetActive(this); if (activateVfx) Instantiate(activateVfx, transform.position, Quaternion.identity); if (activateSfx) AudioSource.PlayClipAtPoint(activateSfx, transform.position); } public Vector3 GetSpawnPosition() => transform.position; public Vector2 GetForward() => spawnForward.sqrMagnitude > 0.0001f ? spawnForward.normalized : Vector2.right; // Для визуалки можно сделать метод Highlight(bool on) и менять спрайт/свет } }