/// <summary> /// Gets called whenever the controller collides with a sphere. /// Checks whether we are on beat or not and instantiates visual feedback depending on whether this is on/off beat. /// </summary> /// <param name="sphereIndex">The index of the sphere we have collided with</param> /// <param name="sphere">The actual sphere object we collided with</param> public void OnSphereCollision(int sphereIndex, MotionTrackerSphere sphere) { if (sphereIndex == m_nextSphereIndexStruct.nextSphereIndex) { var collisionToBeatDifference = m_metronome.OnBeat(); // Are we on beat? if (collisionToBeatDifference <= m_onBeatThreshold && collisionToBeatDifference >= -m_onBeatThreshold) { // Give some visual feedback Instantiate(m_particlePrefab, sphere.transform); m_gameManager.AddScore(1); } else { // Give some negative visual feedback for missing the beat var particleObject = Instantiate(m_particlePrefab, sphere.transform); var mainSystem = particleObject.GetComponent <ParticleSystem>().main; mainSystem.startColor = new Color(255, 0, 0, 1); Debug.LogFormat("CollisionToBeatDifference: {0}, BeatThreshold: {1}", collisionToBeatDifference, m_onBeatThreshold); } // This will be reset if we are too late currently. // Quickfix for being able to hit a bit before beat m_nextSphereIndexStruct.nextSphereIndex = sphereIndex + 1; // We want to avoid cases where the index is reset back a value due to a callback when we were early m_nextSphereIndexStruct.m_alreadyUpdated = true; } }
public static IEnumerator StartMetronome() { Metronome.secondsBetweenBeats = 60.0f / Metronome.beatsPerMinute; Metronome.nextBeatTime = AudioSettings.dspTime; Metronome.metronomeStarted = true; while (true) { if (Metronome.metronomePaused == false) { double curTime = AudioSettings.dspTime; if (curTime >= nextBeatTime) { Metronome.currentBeatTime = Metronome.nextBeatTime; Metronome.nextBeatTime += Metronome.secondsBetweenBeats; if (Metronome.OnBeat != null) { Metronome.OnBeat(); } } } else { Metronome.nextBeatTime = AudioSettings.dspTime; } yield return(null); } }
// Update is called once per frame void Update() { if (Input.GetButtonDown("Fire1")) { var timing = met.OnBeat(); if (timing < -0.09f) { Debug.Log(string.Format("Closer to next beat {0}", timing)); } else if (timing > 0.09f) { Debug.Log(string.Format("Closer to previous beat {0}", timing)); } else { Debug.Log(string.Format("On beat{0}", timing)); gm.AddScore(1); } } }