Home Game Development Unity Mirror Networking Audio Behaves Differently on Host and WebGL Client

Unity Mirror Networking Audio Behaves Differently on Host and WebGL Client

0
Unity Mirror Networking Audio Behaves Differently on Host and WebGL Client

[ad_1]

I am creating a system in Unity using Mirror Networking which will only require one host/server and one client. It is a WebGL build so the client will connect on a browser. I want to send commands from the server to play audio clips on the client without the client being able to control anything. The audio is questions an avatar is asking the user. Due to another constraint (Lip Sync setup) I have one audioSource and one long audio clip which contains all the questions and is played by pressing the space bar. So rather than playing each audio clip separately, the audio clip is unpaused and paused. It will pause itself after a set time when the question line is finished and then I press the enter key to unpause the audioSource for the next question (and again, each line will pause itself after a set time which is the length of the individual question).

This works on the host when I test it in the Unity editor but when I build the WebGL project and test it having the browser as the client and the Unity editor as the server/host, the browser client behaves differently. It will play/unpause when I press the space/enter keys and it will pause but after the first two lines (which play correctly) it will start to skip ahead a few seconds every time I unpause it. I used the WebGL development build to check that the audioSource.time is correct before it is unpaused but it still will play the wrong part of the audioclip.

In this Networking case are there two different audioSources (One for the server and one for the client)? Though how could the debug log show the correct audioSource.time (in Unity editor and the browser) but then it is unpaused at a different time? Is the client version of the audioSource running in the background so the time is increasing even though the audio has been paused? It doesn’t make sense to me but I am quite new to Unity, especially Mirror and network programming in general. Or could this be some issue with WebGL? I think it probably isn’t a WebGL issue since this part was working in WebGL before I added any networking component to the project.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class SoundTracker : NetworkBehaviour
{
    public AudioSource soundFX;
    public int count = 0;

    // Update is called once per frame
    [Server]
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            count = 1;  // We want to skip the long first line in testing
            startSound();
        }

        if (Input.GetKeyDown(KeyCode.Return))
        {
            playSound(count);
            count = count + 1;
        }
    }

    [Server]
    IEnumerator SoundWaiter(float duration)
    {
        yield return new WaitForSeconds(duration);
        soundFX.Pause();
    }

    [ClientRpc]
    void startSound()
    {
        soundFX.time = 33.5f;  // Not starting at the start so I don't have to wait through a long line every time I test
        soundFX.Play();
        //StartCoroutine(SoundWaiter(33.5f));
        StartCoroutine(SoundWaiter(3.2f));  // The second line is much shorter

    }

    [ClientRpc]
    void playSound(int number)
    {
        Debug.Log("Playing sound: " + number);
        Debug.Log("soundFX.time: " + soundFX.time);
        float[] lineDurations = {3.2f, 4.75f, 4.5f, 5f, 4f, 5.5f, 5.5f, 3.8f, 8.6f, 2.9f};
        soundFX.UnPause();
        StartCoroutine(SoundWaiter(lineDurations[number]));
    }
}

[ad_2]