using System; using UnityEngine; using UnityEngine.InputSystem; public class Note : MonoBehaviour { public int Uid; [Header("节拍数据")] public double beatTime; // 绝对击中时刻 public float approach = 1f; [Header("运行参数")] [SerializeField] private float speed = 3f; private bool judged = false; /* 1. 静态:最近一次空格的 dspTime */ private double lastSpaceTime = double.NegativeInfinity; private double spawnTime => beatTime - approach; private float spawnY = MusicGame.instance.judgeLine.y + 8f; private float judgeY => MusicGame.instance.judgeLine.y; private bool canMove = false; void Start() => canMove = false; void Update() { double dsp = AudioSettings.dspTime; // ① 捕获空格时间戳(只抓第一次) if (Keyboard.current.spaceKey.wasPressedThisFrame) { lastSpaceTime = dsp; judged = false; Debug.Log("空格时间戳:" + lastSpaceTime); Debug.Log("当前时间:" + beatTime); } // ② 到出生时刻才动 if (!canMove && dsp >= spawnTime) canMove = true; if (!canMove) return; // ③ 音频时间驱动位置(带平滑) float targetY = spawnY - speed * (float)(dsp - spawnTime); transform.position = new Vector3(transform.position.x, targetY, transform.position.z); // ⑥ 兜底:掉出屏幕 if (targetY < judgeY) { Debug.Log($"{dsp - lastSpaceTime}"); if (dsp - lastSpaceTime <= 0.25f && !judged) { Debug.Log($"音符 {Uid}击中"); judged = true; lastSpaceTime = double.NegativeInfinity; MusicGame.instance.Sroce += 10; MusicGame.instance.onNoteHit(Uid); } else { MusicGame.instance.onNoteHit(-1); Debug.Log($"Miss 音符 {Uid}(超时)"); } Destroy(gameObject); } } }