I’m currently working on a game that is going to be based on music, therefore I will need precision timing. I’m looking for a way to get around slowdown. If the frame rate ever drops, then the music will no longer be in time with the game.
My question is: What can I do to make the game skip frames during slowdown so the game stays in time with the music?
To elaborate: the problem is that what happens in your game is (apparently) frame rate dependent. The trick is to make everything happen based on time rather than frame. Time.deltaTime is a value that can be used to remove frame rate dependency from your game mechanics.
As an example: if you move an object in Update as follows:
// move object 1 unit per frame
transform.x += 1;
When placed in Update, this code will be frame rate dependent. To remove this dependency you could change it to:
// move object 'mySpeed' units per second
transform.x += Time.deltaTime * mySpeed;
You can then add the variable mySpeed to your class and use it to control the speed with which your object moves.
Obviously, if you have many frame rate dependent things in your game already, it will be quite some work to remove all the dependencies. It will be worth it, though, as frame rate dependency is a very big problem (as you already found out with your music clip).
Hmmmm, deltaTime doesn’t seem to be cutting it for me.
I’m doing the same thing - trying to sync with music - and am having troubles due to variable frame rates.
I’ve run this test on the computer and the iPhone:
Basically, I send an object along the x axis at a speed of 10*time.deltaTime. I start it at the same time in the music track, then when the object gets to the target x position, I display the current time of the music track.
If I continuously repeat this process, I’ll start to see deviations. Anybody know why this is, and how to get it more precise so variations in frame rate doesn’t impact it so much (I though deltaTime would do the trick)?
Where are you performing the movement code? If not in Update or FixedUpdate, Time.deltaTime is not the value to use.
Is your Time.timeScale set to 1? If it isn’t, Time.deltaTime is not in seconds.
If your code does something like this:
x += 10*Time.deltaTime;
if (x > endPosition)
x = 0;
you actually lose part of the deltaTime of the frame where the end position is reached. You should incorporate the “overshoot” into the new x position. If you don’t the small fractions of deltaTime you lose, will add up and eventually you’ll run out of sync with the real time (the lower the frame rate, the sooner you’ll notice the out-of-syncness).
I do the movement code in Update() via a Transform along the x axis of 10.0 * time.deltaTime.
I basically shoot the object down until it hits a specific x then record the time. I do this once each time; basically relaunch the app for each new test (as I found there is a lot of stutter when the app first starts).
I’m bringing this topic back up because I’ve had no success in my attempts. When the framerate drops in the game, the object’s movement (which is being multiplied by Time.deltaTime) will become out of sync with the music. I’ve set up a little test to try and get this working but no go. I will be attaching a zip of the test for anyone to check out in a little while.
Here is the test. The sphere moves based off of Time.deltaTime and will turn the cubes blue when it contacts them. This is to happen to the beat of the music. The issue is, when the framerate drops, the sphere moves too slow and gets out of sync. I have been under the impression that Time.deltaTime avoids this issue, but apparently not.
After scouring the forums I came upon a variable called AudioSource.time. This shows the current time in the audio being played. So, I created a new delta time: AudioTime. Instead of tracking the time passed since last frame, I track the amount of time passed in the audio source since last frame. This gives me a number which I can multiply everything by. It’s a little skippy, but runs solid no matter the framerate. I’ve included a zip of the assets for anyone to use.