- Home /
Does Unity 5 help with rhythm games? Or should I stick to AudioSource.PlayScheduled?
I'm currently trying to make a simple metronome, based on a BPM (say 60). The metronome will play a sample every 1 seconds. The user can press a key when they hear a sound, and the application will say how accurate their key press was against the metronome.
Is there anything in Unity 5 to help with this type of rhythm game scenario? Or should I stick to AudioSource.PlayScheduled? Obviously using coroutines that are dependant on frames is a bad idea, so I've moved away from that...
Any help is greatly appreciated!
Thanks.
I have written a rhythm game library for a personal project. You can find the code and documentation here. It was written for Unity 4, so you might have to update a few things, but do look through the code to see how things are done. Feel free to ask if you have any questions.
The library is based off of using AudioSource.timeSamples
for ti$$anonymous$$g ins$$anonymous$$d of Time.time
.
Hi _Gkxd - thanks a lot for providing the code and documentation. It will hopefully provide me with a good way of understanding how these games are built from foundation. I did start off with Time.time and using a delta to correct for any lag. However I'll try using AudioSource.timeSamples.
I guess both AudioSource.timeSamples and AudioSource.PlayScheduled could work, depending on the implementation. But it seems like AudioSource.timeSamples would be great for a longer audio file.
Again, thank you for offering to help, I greatly appreciate it.
Answer by pablo_leban · Apr 15, 2015 at 09:08 AM
Im making a rhythm game too right now. The metronome is easy. First you need to know the BPM of the song, let's say 120 BPM. Then because 120 BPM are 120 beats in 60 seconds you can say that: 120/60 = 2. This means that there are 2 beats every second. If you know this, then you can get the samples of each beat and doing that, you can store that number in a variable and do this:
public double BPM = 120;
public double beatSamples;
public double nextBeatSamples;
void Start(){
//44100 are the sample rates, assuming your audio is in 44100hz
beatSamples = (44100 / (BPM / 60));
//beatSamples = 22050
}
void Update(){
//audio.TimeSamples will increase when the song is playing
if (audio.timeSamples >= nextBeatSamples && audio.isPlaying) {
//plays the metronome sound
audio.Play();
//here it increases the nextBeatSamples, so it will not enter the If
//again and it will wait until the timeSamples
//get to the new nextBeatSamples
nextBeatSamples += beatSamples;
}
}
void Update() is called every frame, would that be reliable for a long song? The reason i'm asking is that i've been told that coroutines are a bad way of ensuring accuracy. However, I can't seem to find any info on whether or not void Update() is a coroutine or not. If not, then great, I'll look into using audio.timeSamples!
Update() it is not a coroutine. It doesn't matter if the song is too long or not. Update just makes a check of two numbers between the audio.timeSamples and the nextBeatSamples, and if it does it only increase a variable. It's not creating objects or something like that, which will not create any kind of lag.
Thanks pablo_leban, I understand now why you'd want to check BP$$anonymous$$ against audio.timeSamples as it provides you back the time in samples :) Thanks for providing this elegant solution, I'll post any updates as I go along!
Answer by DHARMAKAYA · Dec 26, 2015 at 11:13 AM
Looks like a wonderful script and also nice info! Likely try this later today!
Hey, any reliable onset beat detection script anywhere?
Thanks in advance! @pablo_leban
Your answer
Follow this Question
Related Questions
Calculating rhythm of any music? 3 Answers
Does anyone know of a tutorial for a rhythm game? 0 Answers
Lag/jerkiness (as opposed to low framerate) in a Rhythm game 2 Answers
Spawn object, or not, depending on audio properties 0 Answers
Simple rhythm question. 2 Answers