- Home /
Timing problem in Guitar Hero style game
I have a problem with my rythm game I just cannot work out. I shall try to explain logically.
so each beat (or fret) is 1 unit long, and when you transform.position.z -= Time.deltaTime it moves at 1 unit per second. this is great, so i have 60 bpm and 120 bpm straight from the physics.
there are 2 main vars - BPM and AdjustNotes : BPM - speed of song (beats per minute) : AdjustNotes - this is the co-efficient where the problem lies. more in a moment.
Here is the published project (keys are a s d f) : http://www.alucardj.net16.net/rythm1-1/WebPlayer.html
so change BPM to 60 and play scene. all the notes are inst'd in perfect time. change BPM to 120 and play, also the notes are happily in place.
Now change the BPM to 90 or 150 or 210(for a faster display of effect). the notes become inst'd earlier and earlier. Now change Adjust notes to 0.5 , the notes now become inst'd later.
So i have found where this co-efficient lies, but now what do i factor in to compensate for BPM's ?
I am imagining a graph (the terrible drawing as attached) , then how do I calculate AdjustNotes ???? http://www.alucardj.net16.net/rythm1-1/Adjust%20Notes%20co-efficient.jpg
and where did I get 0.499 from ?! ( Time Manager is set to 0.001, so 0.5(1/2 a beat) - 0.001 = 0.499 )
BUT the AdjustNotes value of 0.499 is incorrect too , as if the tempo is increased to 3 x 60 = 180 BPM , the notes start to lag.
I really cannot find the compounding factor, please help.
Here are all my scripts (they arn't that bad to read!) :
Song Manager : http://www.alucardj.net16.net/rythm1-1/scriptSongManager.js
Note 1 : http://www.alucardj.net16.net/rythm1-1/scriptNote1.js
Note 2 : http://www.alucardj.net16.net/rythm1-1/scriptNote2.js
Note 3 : http://www.alucardj.net16.net/rythm1-1/scriptNote3.js
Note 4 : http://www.alucardj.net16.net/rythm1-1/scriptNote4.js
Fretboard : http://www.alucardj.net16.net/rythm1-1/scriptFretboard.js
Edit :
I have changed the scripts so that any movement is applied After all calculations and Instantiating have been done , but it has the same effect as the question. (above script links are now to updated scripts)
I have also added a top-down view of the project , to make the problem easier to see : http://www.alucardj.net16.net/rythm1-1/rythm1-1tv.html
Song Manager script : http://www.alucardj.net16.net/rythm1-1/scriptSongManager.js
Also, with the suggestions I've received regarding multiple scripts : this is infact something I've looked at. But with the Time Manager set to 0.001 there should be less time for this compounding error to build up so drastically. And even with each coloured note having it's own script, they all move together and the error is the same with all of them. Also, the notes stay in relative position to the fretboard (on another script using the same timeScale var). So I am sure whatever is happening, it's really not the separate scripts.I'm sure it has something to to with the co-efficient that is multiplied where the calculation is done to find the 1/8 beat , and then the Instantiating of the notes within the Main script http://www.alucardj.net16.net/rythm1-1/scriptSongManager.js
e.g. I'm almost certain the problem is Here :
function FixedUpdate()
{
// set timeScale from BPM on the fly
timeScale = (BPM / 60) * Time.deltaTime;
eighthBeatTimer += timeScale;
// check if on 1/8 beat
if (eighthBeatTimer >= AdjustNotes)
{
//check arrays , play notes for flagged
if (SongPosition >= SongSheet1.length) {SongPosition = 0;}
if (SongSheet1[SongPosition] == 1) {Instantiate (Note1, Vector3(-0.5, 0.075, 10), Quaternion.identity);}
// other notes instantiated also , see full script
SongPosition++;
eighthBeatTimer = 0.0;
}
}
Why do you have 4 different scripts for the four notes? They're exactly the same as each other!
And as for something actually related to your problem... you shouldn't assume that FixedUpdate is actually as 'fixed' as it claims. $$anonymous$$eep in $$anonymous$$d that it isn't actually perfect.
the input for each note (a s d f) and the point (1 2 3 4), so the scripts are NOT exactly the same. I am still learning, and know there's better ways (enum). but it's working except for the compounding lag error.
true the fixed updates are not exact, but on each frame this is compensated for (timeScale = (BP$$anonymous$$ / 60) * Time.deltaTime;) timeScale var is then used on all scripts to keep sync. note - i have also changed the Time $$anonymous$$anager for fixed update loops from 0.02 to 0.001 . it seems to be managing fine.
sry, I do appreciate the comments, but i have spent days trying to work this out and am a little frustrated. am not sure whether it's where the notes are instantiated in the script, or where the timeScale is added, or if there is another factor i cannot see at work. Thx for reading anyway :)
for anyone looking at this, set the BP$$anonymous$$ to 210, it makes the lag effect appear quicker. Also if anyone wants I can do a publish from a top view. * top-down view now included in the question.
Well, I can't really explain your problem, but I can say that unnecessary script duplication is bound to cause you issues in the future. You should just make the key code a public variable, and change it on an instance-by-instance basis.
Is it possible the problem is a Culmative Floating Point Error? http://answers.unity3d.com/questions/226901/problems-with-float-in-for.html
Answer by AlucardJay · Apr 15, 2012 at 06:41 AM
By golly gosh , I think I've cracked it ! Reiterating the problem helped me solve it, which is :
At the end of the FixedUpdate I was returning the var eighthBeatTimer to a fixed absolute value of 0.0;
By simply changing this line to :
//eighthBeatTimer = 0.0;
eighthBeatTimer -= AdjustNotes; // testing not returning to absolute value
(I also had to change AdjustNotes back to 0.5 , as it should be)
I set the BPM to 210, hit play and made a cuppa. When I came back the notes were still in perfect time and in correct relation to the fretboard.
Thankyou all who read the question, thought about it and gave suggestions.
Now I can move on to the important part. To some that would be the graphics , but to me it's the $$anonymous$$usic =]
Woo-Hoo !
Answer by Dacoke · Jul 18, 2014 at 04:21 PM
Hi there :) Unfortunately your links are dead now but I was wondering if you managed to finish it.
I am actually programming the same type of game but I would like to integrate the use of the motion controller "Leap". I would really appreciate some advice of yours :)
Thanks!
Your answer
Follow this Question
Related Questions
Draw point with time function 1 Answer
Floating point accuracy weirdness 4 Answers
Is there a way to speed up the game more than 100 times? 1 Answer
Time does not start counting down when need to 1 Answer
Character moving 1 Answer