Hi,
I’ve encounter some wired issue about AudioSource playing an audio clip.
I have an audio source that will play an audio clip after level was loaded.
But when I check it’s current playing track time using : AudioSource.timeSamples/AudioSource.clip.frequency, it tells me that the track time is ahead of Time.timeSinceLevelLoaded.
I understand I’ve some small difference cause by frame time difference, but around 0.25s ahead of Time.timeSinceLevelLoaded is really strange.
Here are a simple graph of how my audio playing components link to each others.
GameManager.cs :
public class GameManager : MonoBehaviour
{
public GameObject Player;
public AudioSource BGM;
public static GameManager instance;
bool beatCheck;
float checkBeat = 1f;
public void Awake()
{
if (instance == null) { instance = this; } else if (instance != this) { Destroy(gameObject); }
}
public void Update()
{
float BGMTime = BGM.timeSamples / (float)BGM.clip.frequency;
float BGMBeat = BGMTime / 60f * 140f;
if (BGMBeat >= checkBeat && !beatCheck) {
Debug.LogFormat(gameObject, "At time since level loaded: [{1}s], beat : [{0}beat/ {6}s], at BGM time : [{2}s/ {3}beat], diff is [{4}s/ {5}beat]", Beater.instance.GetCurrBeat(), Time.timeSinceLevelLoad, BGMTime, BGMBeat, BGMTime - Time.timeSinceLevelLoad, (BGMTime - Time.timeSinceLevelLoad) / 60f * 140f, Beater.instance.BeatToSecond(Beater.instance.GetCurrBeat()));
checkBeat += 1f;
}
}
Beater’s Update (Using 140 BPM):
public void Update()
{
timer += Time.deltaTime;
if (!GameManager.instance.BGM.isPlaying) {
GameManager.instance.BGM.Play();
Debug.LogFormat(gameObject, "BGM play at [{0}s /{1} beat /{2} s]", Time.timeSinceLevelLoad, GetCurrBeat(), Time.time);
}
SetCurrBeat(timer * GetCurrBPM() / 60f);
}
And here is some of the log messages I’ve got after running the game :
BGM play at [0s /0 beat /2.190357 s]
Main menu : loading completed !
At time since level loaded: [0.2356176s], beat : [0.6278816beat/ 0.2690921s], at BGM time : [0.4479819s/ 1.045291beat], diff is [0.2123643s/ 0.4955166beat]
At time since level loaded: [0.6497393s], beat : [1.594166beat/ 0.6832138s], at BGM time : [0.8746485s/ 2.040847beat], diff is [0.2249092s/ 0.5247881beat]
At time since level loaded: [1.080409s], beat : [2.599062beat/ 1.113884s], at BGM time : [1.301315s/ 3.036402beat], diff is [0.2209058s/ 0.5154468beat]
At time since level loaded: [1.511001s], beat : [3.603776beat/ 1.544475s], at BGM time : [1.727982s/ 4.031958beat], diff is [0.2169809s/ 0.5062888beat]
At time since level loaded: [1.942202s], beat : [4.609913beat/ 1.975677s], at BGM time : [2.175986s/ 5.077302beat], diff is [0.2337841s/ 0.5454962beat]
At time since level loaded: [2.355902s], beat : [5.575208beat/ 2.389375s], at BGM time : [2.581315s/ 6.023069beat], diff is [0.2254131s/ 0.5259638beat]
At time since level loaded: [2.786634s], beat : [6.580248beat/ 2.820106s], at BGM time : [3.007982s/ 7.018624beat], diff is [0.2213476s/ 0.5164776beat]
At time since level loaded: [3.200802s], beat : [7.546638beat/ 3.234273s], at BGM time : [3.434649s/ 8.01418beat], diff is [0.2338469s/ 0.5456428beat]
At time since level loaded: [3.631663s], beat : [8.551977beat/ 3.665133s], at BGM time : [3.861315s/ 9.009735beat], diff is [0.2296522s/ 0.5358551beat]


Whats the error? if you can paste it, but it should work void Update() { if (Player1ScoreText == 5.ToString()) { SceneManager.LoadScene("Player1Win"); } }
– xxmariofer