Search Unity

Resolved Client side only Component (Audio Source) on Spawned Network Objects

Discussion in 'Netcode for GameObjects' started by azcoffeehabit, Dec 23, 2022.

  1. azcoffeehabit

    azcoffeehabit

    Joined:
    Jun 14, 2015
    Posts:
    13
    Hi Everyone,

    I am trying to set up per-user voice volume control in my project. Voice/volume is currently working with an Audio Source on the Players' Network Objects going to a single Mixer Group "VoiceVolume".

    Now I want to be able to control the volume of each player in the scene by setting the Volume on the Audio Source Component attached to each player Network Object.

    The issue I am running into is that I want to set this volume only for the local client, not on the server, so that changing this value wont change the value for everyone and the server will allow for it to differ from server state.

    I am trying to understand what options I have here.

    Thanks for your time!
     
  2. azcoffeehabit

    azcoffeehabit

    Joined:
    Jun 14, 2015
    Posts:
    13
    It's funny when you look at something for days and then when you finally ask the question you figure out the answer.

    After spawning the Network Object, create and attach the AudioSource Component in code. This will allow for local control of the Component settings and the server doesn't even have to know about it.

    Code (CSharp):
    1.  
    2. private void Awake()
    3.     {
    4.         if(NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsHost)
    5.         {
    6.             AudioMixer mixer = Resources.Load("MainAudioMixer") as AudioMixer;
    7.             string _OutputMixer = "Voice";
    8.             this.gameObject.AddComponent<AudioSource>();
    9.             this.gameObject.GetComponent<AudioSource>().outputAudioMixerGroup = mixer.FindMatchingGroups(_OutputMixer)[0];
    10.  
    11.             source = this.GetComponent<AudioSource>();
    12. //...
    13.