- Home /
Timings and synchronization difference after build
Hi all,
I've been mucking around trying to make my first game in Unity, and I've encountered some problems with timings differences between preview and build versions of the game.
I'm trying to make a simple guitar-hero style clone, except instead of having many notes there is only one note for a drum. The drum is played in a rhythm to a song.
To accomplish this I have abstracted the idea of movement to beats per minute - on frame Update, I try to get the time elapsed since the last frame, and move all notes on the trail forwards by that many beats. The code looks something like this:
// In NoteTrail.cs
void Update () {
float bps = bpm / 60.0f;
float secsSinceLastFrame = Time.deltaTime;
float beatsToMove = bps * secsSinceLastFrame;
foreach (Note note in notesOnTrail) {
note.Advance(beatsToMove);
}
}
// In Note.cs
public void Advance (float beats) {
beatsElapsed += beats;
UpdatePosition(beats * NoteTrail.PIXELS_PER_BEAT);
}
public void UpdatePosition(float dist) {
float currentY = transform.position.y;
transform.position = new Vector3(0.45f, currentY - dist, 0.5f);
}
This seems to work well on my computer - the song is 165 bpm, so in my code I play the start of the song on load, wait n beats, then start the trail moving.
However, as soon as I build the project, the timings go really weird - the notes appear to be moving faster, and other sound triggers appear laggy (these are triggered as coroutines). This occurs both when building locally (for mac), and when building as an app for Android. Again, I do not get this problem when running the game in the preview.
What is it about the process of building that could cause these discrepancies? I had hoped that by using Time.deltaTime this would not be a problem.
Any kind of advice on timings would be helpful :)
Luke
(I can provide more context on how the code works if needed!)