Using setFrequency
is definitely the thing to use for matching the playback speed to the player’s speed. The specificity of the correlation between x position and the audio’s playback position is where the complexity comes in, depending on your exact needs.
A simple implementation might do something like just rely on setFrequency
when the player is moving, and then at specific x values/checkpoints/respawn, manually set the audio’s playback position with setPosition
to an exact synchronized value. Another good time to sync like this would be when the player’s speed reaches 0, and as a result no audio is playing.
A more complex implementation might do something like calculate the expected playback position vs the current x value, and then perform some kind of drift compensation to interpolate to the target playback position. This will lead to a “more accurate” match between the x position and playback position, but will introduce audible warping, which may be undesirable. A C# example of the math required for this can be found in the Video Playback example in our Unity docs, specifically in SampleFramesAvailable()
.
Combining both of the above methods is also a possibility, but again, it depends on your exact needs. A couple of other things to note though - I likely wouldn’t call setPosition
every update cycle, but for setFrequency
it should be fine, as 50hz/20ms is the default update period used for FMOD Studio. Also, if you’re wanting to use setFrequency
with negative frequency values for reversed playback when the player moves backwards. the sound you’re playing will have to be created as a sample instead of a stream.