Are rhythm games supposed to be possible or not?

I just did some searches to try and find help, and got conflicting answers from over the course of several years of forum history.

I’m trying to make a 2D sidescroller that rewards you for performing actions (mainly jumping) in time with the rhythm of the music. The “rhythm game” aspect isn’t intended to be all the complicated (beats come about every half second, there’s no variety, just keep your actions falling along the beats).

What I had been using was:

static var rhythmAble = false;

function FixedUpdate () {
	beat += 1;
	if (beat == 10) {
		rhythmAble = true;
	}
	if (beat == 23) {
		rhythmAble = false;
	}
	if (beat == 25) {
		beat = 0;
	}
}

I came up with the numbers via trial and error. 25 fixed frames happened to match up with the beat and 13 frames seemed a reasonable amount of leeway to give to human reflexes. They worked fine for a few weeks in the editor (and I think I even published it a few times and it worked), but then I changed a graphic setting and suddenly it’s out of sync everywhere.

Some older threads said that Unity’s audiosync capabilities just plain aren’t good enough to do a rhythm game. Some newer threads suggested it might be possible but those threads also about fairly simple games that didn’t seem to have bad guys attacking and whatnot which I suspect could complicate things.

A few of the later threads said “use deltaTime” which makes sense but I’m not actually sure how to implement it.

Hello,

Generally FixedUpdate is for physics stuff only. You probably want to use Update and then use deltaTime.

deltaTime keeps the elapsed time since the previous call to Update. This way you know how much time has gone by between calls.
You could also take a look at the other Time class members such as time and timeSinceLevelLoad.

Then you could record the time since the song (or event starts) and from then on keep track of the beat offsets since then.

Yes rhythm games are entirely possible, I’ve made fully dynamic ones in Flash before, and if that can be done then Unity certainly can! As Timmer said, you probably should use Time.deltaTime in the regular Update function and then interpolate the position of the jump based on how close to dead on the beat you are.

Good luck!
Spec

First off, I’m still fairly new to programming so bear with me.

I’m not quite sure how to use Time.deltaTime. I understand that deltaTime is what you need to be framerate independent but I don’t what understand what deltaTime actually IS. I know, “The time in seconds it took to complete the last frame” but then how do the example functions such as

function Update () {
    // Move the object 10 meters per second!
    var translation = Time.deltaTime * 10;
    transform.Translate (0, 0, translation);
}

work exactly? Strictly speaking I’d think the “The time in seconds it took to complete the last frame” would be something like 1/60th a second. Time.deltaTime X 10 should almost always be about equal to 1/6th, which would result in the object in the example object remaining pretty much in the same place. Obviously that’s not how it works so I’m not really sure what deltaTime actually means or how to use it intelligently.

Meanwhile, I’ve been trying using the “AudioSource.time” which seems to be working, using the following code:

var beat = 0.0;
var beatSub = 0;
var beatTot = 0.0;
static var rhythmAble = false;
var guiPop : GUITexture;
var Mo : AudioSource;

function Start()
{
	audio.Play();
}

function Update()
{
	beat = Mo.time;
	beatSub = Mo.time;
	beatTot = beat - beatSub;
	guiPop.pixelInset.x = beatTot*100 + 220;
	if ((beatTot > 0)  (beatTot < .4)) {
		rhythmAble = true;
	} else {
		rhythmAble = false;
	}
	var beatStr = beatTot.ToString();
	guiText.text = beatStr;
}

I subtract the int value of audioTime from the float value, leaving me with a range between 0 and .9999, and then players are rewarded for taking actions when the beatTot value is between 0 and .4. I need to adjust those numbers to actually sync with the music but it seems to be working in general (unless there’s more that I’m not aware of).

No worries on being new – glad to help out :slight_smile:

One way to think of deltaTime is if you’ve taken any physics classes, the deltaTime is like your t variable in kinematic equations.

Basically to update an object’s position you need to know 1) original position, 2) speed and 3) elapsed time. You end up with the equation that says “an object’s position based on time is that of the starting position plus it’s speed multiplied by the amount of time it’s been traveling”.

In Unity code this would be:

// assuming just moving on X-axis
transform.location.x = transform.location.x * speed * Time.deltaTime

Where speed is some defined variable. In your pasted code example, it is 10.

Since it’s running this every frame, as you say deltaTime will be say 1/60th of a second so every update it will be moved Speed * 1/60 units, which of course is the same as 10 Units/second which is what we want.