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 (!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 JudgePerfect() { //Debug.Log($"Track {trackIndex}: Perfect!"); isHit = true; Destroy(gameObject); } private void JudgeGreat() { //Debug.Log($"Track {trackIndex}: Great!"); isHit = true; Destroy(gameObject); } private void JudgeGood() { //Debug.Log($"Track {trackIndex}: Good!"); isHit = true; Destroy(gameObject); } private void JudgeMiss() { //Debug.Log($"Track {trackIndex}: Miss!"); isHit = true; Destroy(gameObject); } }