Sunday, December 25, 2016

Christmas Jam 2015


I've Spent the last two days of my break making this little Christmas game.
The main point of this game was to make a fun game for my friends.

But more importantly, i now have written a script to synchronize game timers to music (most music synchronizes cost upwards of 30$ on the asset store).


So why cant i just count updates or use system time? Good question. I'm sure you know that system updates on computers are fairly irregular, so imagine you are trying to play a song using updates as your timer, this would be like using a metronome that speeds up and slows down at random.

What i ended up doing was actually fairly simple, it is possible to expose the time sample variable within an audio source component in unity, however due to the fact that the time samples are increasing faster than the computer can update, you will get irregular values each play session.
Here is an example:

system time - time samples
play session 1
t0 - 0
t1 - 100
t2 - 1490
t3 - 4903
...
play session 2
t0 - 0
t1 - 123
t2 - 1503
t3 - 5056
...

What i ended up creating was a script that checks a for loop each update, it checks if the time samples are further along than where i want to something to happen ( which seems like it wouldn't be very on time, but remember how fast these updates are happening, it will only ever be off by a millisecond at most) It then checks that item off of the list as "has happened" and no longer checks to see if that moment should happen, as it already has. ( if i did not do this, once an even happened it would continue to happen every update after, making my bullet hell completely impossible). I also could have made it so the function yields while waiting for the trigger time to be larger than the time sample.

Mostly Im just surprised that this is no where in unity's documentation.


for (int i = 0; i < Spawners.Length; i++) {
if(song.timeSamples > Spawners[i].TriggerTime){
SongEvent(Spawners[i]);
}
}



No comments:

Post a Comment