using UnityEngine; public class Notes : MonoBehaviour { public int trackIndex; // 轨道索引 public float hitTime; // 该音符应该被击中的时间 private bool canBeJudged = false; // 是否进入判定区域 private bool isHit = false; // 是否已被击中 private void Update() { // 只有进入判定区后,才允许自动miss if (canBeJudged && !isHit && Time.timeSinceLevelLoad > hitTime + 0.3f) { JudgeMiss(); } } private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("JudgmentLine")) { canBeJudged = true; } } private void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("JudgmentLine")) { canBeJudged = false; } } public void JudgeNote() { if (!canBeJudged || isHit) return; // 仅在进入判定区后才能被判定 float currentTime = Time.timeSinceLevelLoad; float timeDifference = Mathf.Abs(currentTime - hitTime); if (timeDifference <= 0.05f) { JudgePerfect(); } else if (timeDifference <= 0.1f) { JudgeGreat(); } else if (timeDifference <= 0.2f) { JudgeGood(); } else if (timeDifference <= 0.3f) { JudgeMiss(); } } private void Recycle() { // 如果存在 NotePool,优先归还池,否则销毁物体 if (NotePool.Instance != null) { NotePool.Instance.ReturnNote(gameObject, "red"); // color 未保存时默认 red,建议上层调用时传入 } else { Destroy(gameObject); } } private void JudgePerfect() { Debug.Log($"Track {trackIndex}: Perfect!"); isHit = true; Recycle(); } private void JudgeGreat() { Debug.Log($"Track {trackIndex}: Great!"); isHit = true; Recycle(); } private void JudgeGood() { Debug.Log($"Track {trackIndex}: Good!"); isHit = true; Recycle(); } private void JudgeMiss() { Debug.Log($"Track {trackIndex}: Miss!"); isHit = true; Recycle(); } }