unity – Rhythm recreation, Sync music begin with the beginning of the extent in fixedUpdate

unity – Rhythm recreation, Sync music begin with the beginning of the extent in fixedUpdate

[ad_1]

I’m engaged on a rhythm primarily based android recreation comparable like Guitar Hero.

I’ve a kinematic rigidbody transferring downwards with tapobjects (colliders) inside which have to be pressed when moved above the button.

The massive drawback: doesnt matter what im making an attempt, i cant get the primary motion of the rigidbody to be aligned with the precise begin of the music. There’s at all times a delay of round 100ms and extra.

Intialization:

void Begin()
{
    velocity = beatTempo / 60f;
    startDelayedBy = 0f;
    setupAudioInputLag = false;
    firstFixedUpdate = false;
    songLag = 0.0692f;   // time in seconds when first beat happens of the audio
    songLagStep = ( beatTempo / 60f ) * songLag; // calculate the gap of songlag on y axis
    velocityAdd = new UnityEngine.Vector3(0f, -velocity, 0f); 
}

What i attempted, Instance 1:

non-public void FixedUpdate()
{
    if (setupAudioInputLag == false)
    {
        beatMusic.PlayDelayed(1f);
        rb.place = new UnityEngine.Vector3(rb.place.x, 0f + songLagStep, rb.place.z);
        setupAudioInputLag = true;
    }
    if (startDelayedBy >= 1f) 
    {
        rb.MovePosition(rb.place + (velocityAdd * Time.fixedDeltaTime));
    }
    if (setupAudioInputLag)
    {
        startDelayedBy += Time.fixedDeltaTime;
    }
}

Instance 2:

non-public void Replace()
{
    getAccurateDspStart = AudioSettings.dspTime;
    if (setupAudioInputLag == false)
    {
        beatMusic.PlayScheduled(AudioSettings.dspTime + 1.0f);
        dspTimeStore = AudioSettings.dspTime + 1.0f;
        rb.place = new UnityEngine.Vector3(rb.place.x, 0f + songLagStep, rb.place.z);
        setupAudioInputLag = true;
    }
}

non-public void FixedUpdate()
{
    if (getAccurateDspStart >= (dspTimeStore) && setupAudioInputLag) 
    {
        rb.MovePosition(rb.place + (velocityAdd * Time.fixedDeltaTime));
    }
}

Instance 3:

non-public void Replace()
{
    getAccurateDspStart = Time.time;
    getAccurateDspStartOld = AudioSettings.dspTime;
}

non-public void FixedUpdate()
{
    accurateDspTime = getAccurateDspStartOld + (Time.time - getAccurateDspStart);
    if (accurateDspTime != 0f)
    {
        if (setupAudioInputLag == false)
        {
            beatMusic.PlayScheduled(accurateDspTime + 1.0f);
            dspTimeStore = accurateDspTime + 1.0f;
            rb.place = new UnityEngine.Vector3(rb.place.x, 0f + songLagStep, rb.place.z);
            setupAudioInputLag = true;
        }
    }
    if (accurateDspTime >= (dspTimeStore) && setupAudioInputLag)  
    {
        rb.MovePosition(rb.place + (velocityAdd * Time.fixedDeltaTime));
    }
}

And rather more i attempted over a number of days witouth even shut of success. I would like accuracy about 10-20ms.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply